Difference between Abstract class and Interface in C# .Net

Interfaces are essentially having all method prototypes no definition but Abstract class can contain method definations also.

In short Interface is a abstract class having all methods abstract.

Both abstract classes and interfaces are used when there is a difference in behaviour among the sub-types extending the abstract class or implementing the interface.

When the sub-types behaviour is totally different then you use an interface, when the sub-types behaviour is partially common and different with respect to the supertype an abstract class is used. In an abstract class the partially common behaviour is given a concrete implementation. Since there is no common behaviour between an interface and a sub-type an interface does not have an implementation for any of its behaviour.

If you create a abstract class writing the abstract keyword in the declaration part then You only can inherit the class. You can not create an instance of this abstract class but can inherit the class and with creating the instance of the derived class you can access the method of the abstract class.

If you use a virtual keyword in a method then you can override this method in the subclass if you wish..

If you create a abstract method then you must override this method in the subclass other wise it shows error in the program.

32 thoughts on “Difference between Abstract class and Interface in C# .Net

  1. The concentration moved away from Interface towards Abstract Class. Better provide some examples… those would add extra values to the explanation.

    • interface is fully abstract.means interface only and only contain the declaration of the methods,but definition implemented in the class.

  2. Good Post. Must read to get the Laymen Definition of the Interface and Abstract Class. A piece of code would make the post much much better.

  3. Momin Ashfaque :
    I am very confused in abstract class and interface in c#.net ,so if any good example or idea about that topic so send on my e-mail.

    • An abstract class can have abstract members as well non abstract members. But in an interface all the members are implicitly abstract and all the members of the interface must override to its derived class.

      //All methods are automaticall abstract

      int AddNumbers(int Num1, int Num2);
      int MultiplyNumbers(int Num1, int Num2);
      Defining an abstract class with abstract members has the same effect to defining an interface.
      The members of the interface are public with no implementation. Abstract classes can have protected parts, static methods, etc.
      A class can inherit one or more interfaces, but only one abstract class.
      Abstract classes can add more functionality without destroying the child classes that were using the old version. In an interface, creation of additional functions will have an effect on its child classes, due to the necessary implementation of interface methods to classes.
      The selection of interface or abstract class depends on the need and design of your project. You can make an abstract class, interface or combination of both depending on your needs.

  4. interface class cannot implement Method only declare signature. ans support for multiple inheritance ,not use abstract or override the method
    Abstract class both have type method ,but abustruct method only give the definition not implement.but other methods is your choice

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s