|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
#1 |
|
Barefoot on the Moon!
Staff
Premium Member
Join Date: Aug 2002
Location: Northeastern USA
Posts: 13,385
|
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.
|
|
|
|
|
|
#2 |
|
Professional gadfly
|
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. |
|
|
|
|
|
#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 |
|
|
|
|
|
#4 |
|
Barefoot on the Moon!
Staff
Premium Member
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? |
|
|
|
|
|
#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 Code:
loop over array of animals animal.speak() endloop 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. |
|
|
|
|
|
#6 |
|
Gremlin Overlord
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 |
|
|
|
|
|
#7 |
|
Barefoot on the Moon!
Staff
Premium Member
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?
|
|
|
|
|
|
#8 |
|
Gremlin Overlord
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 |
|
|
|
|
|
#9 | |
|
Professional gadfly
|
Quote:
|
|
|
|
|
|
|
#10 |
|
Barefoot on the Moon!
Staff
Premium Member
Join Date: Aug 2002
Location: Northeastern USA
Posts: 13,385
|
Okay, I think I get it now. Thanks for all your help, guys
|
|
|
|
|
|
#11 |
|
Gremlin Overlord
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 |
|
|
|
|
|
#12 |
|
Professional gadfly
|
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.
|
|
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|