SIGN UP MEMBER LOGIN:    
ARTICLE

Abstract Class vs Interface

Posted by Prasoon Articles | C# Language June 10, 2009
This tutorial will try to explain difference between Abstract Class and interface theoratically and programitically both ways.
Reader Level:


Probably "Difference Between abstract Class and Interface" is the most frequent question being asked in .Net world .  In this tutorial, I will explain the difference theoretically followed by code snippet.
Theoretically there are basically 5 differences between Abstract Class and Interface which are listed as below :-

  1. A class can implement any number of interfaces but a subclass can at most use only one abstract class.
  2. An abstract class can have non-abstract Methods(concrete methods) while in case of Interface all the methods has to be abstract.
  3. An abstract class can declare or use any variables while an interface is not allowed to do so.

    So following Code will not compile :-


    interface TestInterface

    {

        int x = 4;  // Filed Declaration in Interface

        void getMethod();

     

        string getName();

    }

     

    abstract class TestAbstractClass

    {

        int i = 4;

        int k = 3;

        public abstract void getClassName();

    }

    It will generate a compile time error as :-

    Error    1    Interfaces cannot contain fields .

    So we need to omit Field Declaration in order to compile the code properly.

    interface TestInterface
    {
        void getMethod();
        string getName();
    } 

    abstract class TestAbstractClass

    {

        int i = 4;

        int k = 3;

        public abstract void getClassName();

    }

    Above code compiles properly as no field declaration is there in Interface.

  4. An abstract class can have constructor declaration while an interface can not do so.

    So following code will not compile :-

    interface TestInterface

    {

        // Constructor Declaration

        public TestInterface()

        {

        }

        void getMethod();

        string getName();

    }

     

    abstract class TestAbstractClass

    {

        public TestAbstractClass()

        {

        }

        int i = 4;

        int k = 3;

        public abstract void getClassName();

    }

    Above code will generate a compile time error as :-

    Error    1    Interfaces cannot contain constructors 

    So we need to omit constructor declaration from interface in order to compile  our code .

    Following code compile s perfectly :-

    interface TestInterface

    {

        void getMethod();

        string getName();

    }

     

    abstract class TestAbstractClass

    {

        public TestAbstractClass()

        {

        }

        int i = 4;

        int k = 3;

        public abstract void getClassName();

    }

  5. An abstract Class is allowed to have all access modifiers for all of its member declaration while in interface  we can not declare any access modifier(including public) as all the members of interface are implicitly public. 

    Note here I am talking about the access specifiers of the member of interface and not about the interface.

    Following code will explain it better :-

    It is perfectly legal to give provide access specifier as  Public (Remember only public is allowed)

    public interface TestInterface

    {

        void getMethod();

        string getName();

    }

    Above code compiles perfectly.

    It is not allowed to give any access specifier to the members of the Interface.

    interface TestInterface

    {

        public void getMethod();

        public string getName();

    }

    Above code will generate a compile time error as :-

    Error    1    The modifier 'public' is not valid for this item.

    But the best way of declaring Interface will be to avoid access specifier on interface as well as members of interface.

    interface Test

    {

        void getMethod();

        string getName();

    }

Login to add your contents and source code to this article
Article Extensions
Contents added by Kanaiyalal Prajapati on Jun 11, 2009
Once you say - Interface can't provides method implementation so why putting method definitions?

Class is not an Interface and an Interface is not a class.

Read this :


http://www.codeproject.com/KB/architecture/Abstract_Interface.aspx
share this article :
post comment
 

Hey we use interfaces because we get the advantage of multiple inheritance. C# allows multiple inheritance through interfaces but not through classes.

Posted by Shivanand Arur Jul 11, 2011

Nice Article

Posted by priya rishi Feb 03, 2011

Hi, you can go the following link to find your question's ans http://www.codeproject.com/KB/cs/abstractsvsinterfaces.aspx http://stackoverflow.com/questions/239127/exact-use-of-abstract-class

Posted by rp singh Jan 11, 2011

Hi,
I want to add something in point-3.
Point-3 states - "An abstract class can declare or use any variables while an interface is not allowed to do so."
But interface can have Properties. This point has to be added in Article.
Please Correct me if I'm wrong.!!!

Posted by Nilay Joshi Sep 09, 2010

Hi ,

    Can any one plz tell me what is the exact use of Interfaces,In which scenario we use interface and in which case we use abstract class,Plz explain with an example.....
If we can achieve our goal by using abstract class,y do we use interfaces?????

Posted by Akhil Mittal Sep 03, 2010
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Gauge for SharePoint
Become a Sponsor