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

Posted on 2011-04-25
Last Modified: 2013-12-16

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
Question by:mu_ravi1
    LVL 16

    Accepted Solution


    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:

    LVL 43

    Expert Comment

    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.
    LVL 15

    Expert Comment

    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.

    LVL 3

    Expert Comment

    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

    Author Closing Comment

    not suffcient

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now