I would like to know what makes a class to be called as an abstract class. I believe, abstract keyword definitely make a class, but if one takes out the keyword, then we can create the instance of the class.
In otherwords, what are the characteristics of the abstract class.
Thanks in advance.
-Harsha
An abstract class has one singular characteristic: no instances of the class can be created. Abstract classes are designed to be used as base classes for other abstract and non-abstract classes.
I don't think anybody has said this yet, but I believe the distinguishing characteristic of an abstract class is:
The coder knows how X% of the class is implemented, and how (100-X)% of the class should be implemented, but which cannot be.
This is what distinguishes the abstract class. Interfaces describe how a class should be implemented, but do not give implementors any code or support. Base classes with virtual methods provide default implementations that inheritors may possibly override.
It is only the abstract class that can provide a partial implementation, but cannot provide a complete implementation.
An abstract class serves two related purposes:
The main purpose of an abstract class is to serve as a base class for descendant classes, providing perhaps some common functionality, but at least providing a common set of methods that the descendants has to implement.
For instance, you could create an abstract class Fruit, with an abstract method Eat. Since you cannot actually eat a "fruit", you need to eat a specific type of fruit, the class is abstract and you will never see an object that is labelled "Fruit" in the wild.
Instead, you will see objects of type Apple, Banana, Orange, etc. that are descendants of Fruit, and which have an actual implementation of Eat.
Other than the two things at the top, there is nothing special about an abstract class.
An abstract class does not form a concrete object in the real world unlike pure implementation classes. Abstract as the name suggestes they hold/define common behaviours of related objects that need to be reused/defined independantly in all related objects.
Take an example of Birds. If you are writing a progrm that will have something to do with the birds, then you'll first have an abstract base class as Bird and each bird deriving from the abstract base class Bird. Do note that abstract class BIRD does not represent a concrete real world object but a type of related objects that is birds!
Lets start with the class-diagram and then some code.
alt text http://ruchitsurati.net/files/birds.png
For the
abstract
on the class to have any meaning, it must have abstract members that need implementing, otherwise it's extraneous (unless you intended to prevent instantiation) and you should remove it.It might have an abstract property an inheritor needs to declare, like this
or a method it must implement:
You would use it if the class itself was never to be instantiated but you needed more than an interface, for example base properties and you wanted to use it later: