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. 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.
  3. 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.
  4. Event: Both can contain event. Interface can have only signature of event while abstract class can contain concrete event.
  5. 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.
  6. 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 members 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.
Constructors Interface does not supports constructors. It doesn’t requires it because interface cannot have data field being initialized. Abstract class supports constructors. The main purpose of constructors is to initialize it’s data fields while it is extended by derived class and derived class instantiated (which in turn instantiate abstract class – the base class).
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

Interface Vs Abstract Class
2 votes, 4.50 avg. rating (89% score)

13 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

  7. Captain .NET

    You are incorrect about constructors, abstract classes CAN have constructors although they should be marked protected rather than public.

    Reply

    • Jinal Desai

      Thanks man for the good point.
      Corrected.

      Reply

  8. Captain .NET

    Another point, although Interfaces can only define ‘public’ visibility, you can mark the interface itself Internal.

    Reply

    • Jinal Desai

      Correct. But that applies to class or struct as well.

      Reply

      • Captain .NET

        It does also apply to class or struct but your article specifically states that interfaces can only be public which is technically incorrect.

        Reply

        • Jinal Desai

          Gotcha!
          The language I used is misleading. Corrected. Thanks.

          Reply

Leave a Reply

*


*