Ricky White
asked on
When to create an abstract class?
Abstract class is often defined as a class that cannot be instantiated.
I am not clear what is the use of such a class. Why would I want to create a class like that?
Can somebody give a few example situations when somebody should create an abstract class?
Thanks.
I am not clear what is the use of such a class. Why would I want to create a class like that?
Can somebody give a few example situations when somebody should create an abstract class?
Thanks.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
For example, in a current project I have a "xyzCollectionBase" that is abstract. I don't want to ever instantiate this class so it is abstract. This class only contains one method but I do have a bunch of subclasses and this saves me from having to define the same method in each one.
Of course, the "xyz" in my previous comment is not the real name of the class. Another example from a current project is an "xyzDataItem" class. In this one, I have several abstract methods to ensure that the subclasses implement the methods. And it also contains several implemented methods so that the subclasses don't have to.
You can have abstract properties too: public abstract TypeEnum EntryType { get; }
nice answer AndyAinscow
:-)
ASKER
So an abstract class always has to be a base class right? It should not be a child class?
Thanks all!
Thanks all!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>So an abstract class always has to be a base class right? It should not be a child class?
In my example you saw an abstract class was a child of another abstract class. What you can NOT do is create an instance of an abstract class.
eg.
In my example the following does not work.
Reptile r = new Reptile();
In my example you saw an abstract class was a child of another abstract class. What you can NOT do is create an instance of an abstract class.
eg.
In my example the following does not work.
Reptile r = new Reptile();