• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 327
  • Last Modified:

In C# development when should i choose abstract class over ordinery class.


In C# development when should i choose abstract class over ordinery class.
actually i gone through google and iam not able to find the solution.
pls provide me the example when i need to go for abstrat classs.
With Regards
1 Solution
Imran Javed ZiaConsultant Software Engineer - .NET ArchitectCommented:

If you have a situation where you need some basic functionaility common in all classes then it is time to go for inheritence like you need common properties like name, address, dbo and etc, and in some other case where you have need to have createdby, created date and etc common in all classes.

on the other hand when it is about making a class Abstract, it is can be based on many thing specially when you are interested in polymorphism and there is predefined set of instructions/action which must be implemented by every class like for a typical CRUD application then you may define Create/Add, Get/Read, Update and Delete methods.

for more information please review following links:

AndyAinscowFreelance programmer / ConsultantCommented:
Further to the information from IJZ (which is very useful)

An abstract class can NOT have an object of that type.  eg.  If class CAbstract is abstract then the following will not work:
CAbstract obj = new CAbstract();

You can only create objects on classes derived from CAbstract that are themselves not an abstract class.
NavneetSoftware EngineerCommented:
Abstract is a basic templete with basic features and basic requirements you would like to have atleast in all your Classes.
In more simple terms it's having a Templete which you can't sell(create object), but can produce objects from it to sell.

well all the above is very useful. Here is an explained example to clarify more:
1- You have the You are making a drawing application. The base interface is IShape:
interface IShape
 void Draw();
 double Area();
The interface defines a contract for all users that this object(that implements IShape) does support the method Draw.
2- Now the rectangle and square have common implementations in the area. Thus lets have the abstract class:
abstract class FourRightSidesShape : Ishape
 public double Length{get;set;}
 public virtual double Width{get;set;}
 public double Area()
  return this.Length * this.Width;

 public abstract void Draw();
//further methods can be added as well even if not from the bas interface.. they can be either abstract or have implementation.

Now notice that you can not create a FourRightSidesShape obbject. You should inherit a Rectangle or Square from FourRightSidesShape:

class Square : FourRightSidesShape
 public override double Width
  get{return this.Length;}
  set{this.Length = value;}

 public void Draw()
 {//drawing code goes here...

Now you can instantiate Square object. No nee to repeat the Area method for the Square and Rectangle. Just the Draw method.

Shortly, the abstract is like the interface except for that:
 1- it can/may contain implementation for methods and properties.
 2- it may/can contain variables.
 3- another third non functional distinction: it is that it can have protected and/or private members(methods, properties, variables... etc). Either abstract methods or with their implementations.

 Well, if you have any further inquiries, please do not hesitate :)

Mahmoud Al-Hattab
mu_ravi1Author Commented:
not suffcient
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now