Interface Vs Abstract Class

Difference between Interface and Abstract Class is favourite question of interviewer while he wants to evaluate your knowledge about Object Oriented Programming. When we consider .NET, there are some more stuff we can include in the answer of question. .NET has introduced some more features which creates some similarities and differences between the two building blocks of OOPs like .NET introduced Automatic Properties, Extension Methods, etc.

Following is a try to find some real similarities and differences between the two terminology from the perspective of .NET paradigm.

Similarities

  1. Instantiation:     Both cannot be instantiated.
  2. Constructor:     Both cannot have constructor. Interface is just contract, it does not have any concrete stuff. Abstract class means you must inherit it in any non abstract child class and you cannot inherit constructors.
  3. Properties:     Both have properties. Abstract class can have concrete properties. Interface have only signature of property which you need to implement in the implementor class.
  4. Indexer Property:     Both can have indexer property. But again interface can have only signature which we need to implement in the implementor class while abstract class can have definition as well along with the signature of indexer property.
  5. Event:     Both can contain event. Interface can have only signature of event while abstract class can contain concrete event.
  6. Extension Method:     Both can have extension methods. But in both Interface and abstract class extension methods does not have any existence unless you create it’s implementor’s/inheritor’s object using new keyword.
  7. Back Trace:     Both can be useful to back trace all it’s implementors/inheritors. We can assign object of implementor/inheritor to interface/abstract class respectively using new keyword and back trace all the implementor/inheritor.

Differences

Parameter Interface Abstract Class
Inheritance/Implementation A Class can implement multiple interface. .NET does not support multiple inheritance, so a Class can inherit only one Abstract Class.
Default Implementation Interface has only signature, you cannot provide implementation in interface. In Abstract Class you can provide default implementation.
Default Implementation Pros/Cons While adding new stuff to interface, it is nightmare to find all the implementors and implement newly defined stuff. In case of Abstract Class you can take advantage of default implementation.
Access Modifiers (public, private, protected, internal, protected internal) Interface does not have access modifiers. Everything defined inside the interface is assumed public modifier. Abstract Class can have access modifier.
Purpose Interface suits to implement peripheral requirements. i.e. both person and vehicle class can implement IRun interface. Abstract Class suits to implement core requirements that is shared among common type of objects. i.e. Person abstract class can be inherited by Manager, Supervisor, Clerk, Administrator…
When to use It is better to use interface when various implementations share only method signature.
Use interface when immutable contract is required.
Using interface we can design polymorphic heirarchy of value types.

It is better to use abstract class when various implementations of the same kind share common behaviour.
Use abstract class to define common base class for family of types.
Data Fields Interface cannot contain data fields. However in .NET we can have abstract properties in interface. However just like methods you need to implement those abstract properties inside the implementor class. Abstract class can have data fields.
Automatic Properties Interface cannot have automatic properties. You need to implement all the properties defined in interface into all it’s implementors. Abstract class can have automatic properties.

References
http://www.codeproject.com/Articles/258173/Interfaces-Vs-Abstract-Classes
http://www.codeproject.com/Articles/11155/Abstract-Class-versus-Interface
http://www.codeproject.com/Articles/303081/Comparison-of-Interface-and-Abstract-Class-and-Use
http://www.codeproject.com/Articles/23302/Abstract-Classes-Vs-Interfaces
http://stackoverflow.com/questions/504977/why-cant-i-create-an-abstract-constructor-on-an-abstract-c-sharp-classh3

7 Comments

  1. Krunal Patni

    Again very nice article posted by Jinal.

    Thank you very much for providing us such a nice article.

    Reply

  2. Sandeep Peshwani

    Nice read to get the differences clearly. Very useful for understanding basics.

    Reply

  3. Nilesh Makwana

    nice!
    some of the facts read for the first time!

    Reply

  4. Aravinth

    Nice article

    Reply

  5. Ajay Solanki

    Abstract Class can have Abstract Constructor.

    Reply

    • Harsha Prince

      Is it a question or Knowledge Transfer?

      Reply

  6. Harsha Prince

    Very nice and usefull. Knowledge sharable.

    Reply

Leave a Reply

*


*