Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 04-02-2004, 09:32 AM   #1
Barefoot on the Moon!
Staff
Premium Member
 
Force Flow's Avatar
 
Join Date: Aug 2002
Location: Northeastern USA
Posts: 13,385
Question abstract class in java

What is the purpose or what is the abstract class used for? Right now, it looks like it acts like a regular public or private class.
__________________
There are two secrets to staying young, being happy, and achieving success. You have to laugh and find humor every day, and you have to have a dream.
Force Flow is offline   Reply With Quote
Old 04-02-2004, 09:49 AM   #2
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
An abstract class is a class that can't be instantiated. You can never create an instance of an abstract class, so it is very different from public and private classes. Abstract classes are used as parents to derived classes that can be instantiated.

Let's say you are making some kind of graphics program to display certain shapes. All these shapes have certain things in common, while other properties depend on the kind of shape (only a circle has a radius). You could put the properties they have in common in an abstract class called Shape (such as position and color), and then create classes derived from this abstract class (like circle and square).

In this example, you could never create a pure "Shape" object, because it is abstract. However, you could create a Circle object, and since it inherits from Shape, you know it comes with position and color properties.
doctorgonzo is offline   Reply With Quote
Old 04-02-2004, 11:06 AM   #3
Member (7 bit)
 
Join Date: Sep 2003
Posts: 90
this could either all be right or all be wrong. my knowledge of java is shaky.

this is assuming you know what an interface is. i hope you do.

the only difference between an interface and a virtual (abstract) class is that the former can contain no code whatsoever only the prototyping, whereas the latter can implement the code for common functionality.

for example, you could have an interface called Shape, but then each object would have to implement getColor() which you would think would be the same for each shape. if instead, you created an abstract or vitual class called Shape which defines getColor(). if you inherit the class instead of implementing the interface, then all derived classes do not have to define getColor(), but they are capable of redefining it, if i am not mistaken.

the reason for doing this instead of making an interface and defining it for each derived class is to save space in the v-table or whatever Java uses for virtual lookups. i don't think there is much more to the differences between interfaces and virtual classes.

don't quote me, but I think if at least one function in an interface is defined within the interface, then the entire thing must be defined as an abstract class instead. java got rid of multiple inheritence, so interfaces are probably the better route if you are trying to achieve multiple inheritance.

AS
AerynSedai is offline   Reply With Quote
Old 04-03-2004, 11:26 AM   #4
Barefoot on the Moon!
Staff
Premium Member
 
Force Flow's Avatar
 
Join Date: Aug 2002
Location: Northeastern USA
Posts: 13,385
The light of understanding still eludes me...

I still haven't gotten to the interface section in my java book. This just calls other classes/methods, correct?

I still don't quite understand the uniqueness of an abstract class. Is it limited to method/class calls or can it perfrom variable declarations and calculations?
Force Flow is offline   Reply With Quote
Old 04-05-2004, 10:28 PM   #5
Member (7 bit)
 
Join Date: Sep 2003
Posts: 90
well,

an interface is just an abstraction. in OOP you tend to thing of abstractions as things. these things have attributes and methods. this means objects have things and do things.

i always hate these stupid nonsensical examples, but i am going to use one here.

suppose we want to model an animal shop. we want to create a dog, a cat, and a parrot. we could create separate classes called dog, cat, and parrot and have them do all the things we want them to. this is not bad, but it is not great either. suppose we walk into this store and we want to hear the animals speak. well, we have a list of animals so the code is

Code:
loop over array of animals
  if animal is a dog then bark
  if animal is a cat then meow
  if animal is a parrot then squak
endloop
well, plainly this code sucks. the dog, cat, and parrot all do the same thing, so they should have a similar method called speak. we create an interface called animal from which dog, cat, and parrot inherit. from this we say dog is-a animal, cat is-a animal, and parrot is-a animal. the animal interface in turn says any class which inherits from animal must have a speak function. so now the code looks like

Code:
loop over array of animals
  animal.speak()
endloop
now, i know this example is silly and stupid and probably doesn't even help, but interfaces act as a level of abstraction.

another example, a car. you don't store things as a 1999 honda accord, 2001 pontiac firebird, 2004 corvette, etc. you just say car. in OOP this means you create a car interface. this car interface guarantees that all classes that implement this interface are guaranteed to have 4 wheels, an engine, a steering wheels, an ash tray, a paint color, headlights, and so on. the interface also guarantees what the car can do, such as honk, startengine, turn on windshield wipers, roll down windows, shift gears, etc. THE IMPORTANT THING IS the interface does not tell how this works. the class which implements the car interface must determine for itself how to roll down windows whether it be by power windows or the hand crank. again shifting gears is dependent on the actual car, but every car does it.

now, the difference between an abstract class and an interface is that an abstract class actually breaks THE IMPORTANT THING in that it tells all classes which inherit from it how to do something. if we make our car interface into a car abstract class, we can tell how any derived car will do something. for example, all car horns in the US are keyed to the key of C. i bet you didn't know that. so, our honk method would be playC-note(). this guarantees that any object which inherits from the car abstract class will honk in the key of C. note however, that a derived class can say fudge to that and overwrite the honk method with its own. ie, a derived car can say honk = playF-note().

this is a lot of stuff. i am not sure if any is helpful, but instead of me rambling on, please let me know what needs clearing up so i can focus on that. if you understand all the abstract stuff i can give a concrete real life coding example.

AS

Last edited by AerynSedai; 04-05-2004 at 10:30 PM.
AerynSedai is offline   Reply With Quote
Old 04-06-2004, 02:55 AM   #6
Gremlin Overlord
 
Jaggannath's Avatar
 
Join Date: Apr 2003
Location: Australia
Posts: 2,382
An abstract kind of sits above a class (in the above case, a car), and then each time an object is created which comes under this (in this case accord etc.) it inherits all the properties outlined/specified in the abstract (in this case 4 wheels, tires, chassis etc.). Then you can individualise this object
It's almost a template as such... you can reference it, but it can't be run by itself
Jaggannath is offline   Reply With Quote
Old 04-06-2004, 04:45 PM   #7
Barefoot on the Moon!
Staff
Premium Member
 
Force Flow's Avatar
 
Join Date: Aug 2002
Location: Northeastern USA
Posts: 13,385
ok, so you say an abstract class is a template. This means other classes call the various methods defined in the abstract class, correct?
Force Flow is offline   Reply With Quote
Old 04-07-2004, 02:15 AM   #8
Gremlin Overlord
 
Jaggannath's Avatar
 
Join Date: Apr 2003
Location: Australia
Posts: 2,382
Yup, essentially you've hit the nail on the head
I'm assuming here you're talking about an abstract class, and not an abstract data type or method

Basically it is a template which can contain methods which are not abstract (abstract just means it can't be instantiated)
It is something not defined enough to stand alone by itself, but when you add partial descriptions to the existing information the "child classes" can become stand-alone classes
Jaggannath is offline   Reply With Quote
Old 04-07-2004, 10:32 AM   #9
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
Quote:
Originally posted by force_flow2002
ok, so you say an abstract class is a template. This means other classes call the various methods defined in the abstract class, correct?
That's about it. It helps group similar classes together to ensure that they will all share a common set of properties and methods. Personally, I haven't ever used abstract classes in programming, but there are a few places where they would be useful.
doctorgonzo is offline   Reply With Quote
Old 04-07-2004, 11:00 AM   #10
Barefoot on the Moon!
Staff
Premium Member
 
Force Flow's Avatar
 
Join Date: Aug 2002
Location: Northeastern USA
Posts: 13,385
Okay, I think I get it now. Thanks for all your help, guys
Force Flow is offline   Reply With Quote
Old 04-07-2004, 10:23 PM   #11
Gremlin Overlord
 
Jaggannath's Avatar
 
Join Date: Apr 2003
Location: Australia
Posts: 2,382
You've never used them Gonzo?? What language do you normally program in??
Admittedly, come to think of it I've mainly only used it in Java
Jaggannath is offline   Reply With Quote
Old 04-08-2004, 09:34 AM   #12
Professional gadfly
 
doctorgonzo's Avatar
 
Join Date: Jan 2002
Location: Minneapolis, MN
Posts: 6,364
Send a message via MSN to doctorgonzo
I've done programming in VB, C++, and C#. I just have never came across a situation where I needed to use them in a real-world application. If I program long enough, I'm sure that someday I will need it.
doctorgonzo is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 12:51 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2