View Full Version : Abstract Classes
I'm sitting in a lecture right now, and I'm wondering - what's the point of Abstract Classes*?
* Ones that can not, or should never be created
inherritance
so you can create 2 classes which share the same base.
which is handy if you have virtual functions
Marek
15-04-2005, 09:43 AM
I use them all the time.
If you have a whole bunch of classes with properties or methods in common then put those properties or methods in an abstract base class.
That base abstract class can have both abstract and non abstract methods.
So when you derive from the base abstract class you can effectively force your derived classes to behave in a certain way…
i.e. tell them which functions to override and which ones to not override…
That’s how I use them anyway
:whistle:
If you translate them to the real world:
'Vehicle' could be an abstract class, since nowhere in the world would you find a 'vehicle' without some further specilisation.
You'd find a 'Car', which would be a certain implementation of the abstract 'Vehicle' class or a 'Bike', or 'Airplane'.
so you'd have
class Car extends Vehicle
class Bike extends Vehicle
etc.
EDIT: This way you can define some behaviour or properties of a class without actually specifying the implementation. E.g. all vehicle have a speed, some sort of propulsion mechanism etc.
PS. You could also do this with an interface (say 'Drivable') but that's another way to model the world.
Marek
15-04-2005, 11:37 AM
You could also do this with an interface
Yes and what make abstract classes even better is that it can perform the role of both an interface and a normal base class in one, in that common functions can actually be performed in the abstract class itself, like:
public void ChangeDirection(DirectionTypeEnum oDir)
{
* //Implementation here
}
or something like that...
Abstract classes are cool... :max:
vBulletin® v3.7.1, Copyright ©2000-2025, Jelsoft Enterprises Ltd.