Link to home
Start Free TrialLog in
Avatar of Ricky White
Ricky WhiteFlag for United States of America

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.
SOLUTION
Avatar of MajorBigDeal
MajorBigDeal
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of Ricky White

ASKER

So an abstract class always has to be a base class right? It should not be a child class?

Thanks all!
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>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();