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.
- Instantiation: Both cannot be instantiated.
- 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.
- 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.
- Event: Both can contain event. Interface can have only signature of event while abstract class can contain concrete event.
- 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.
- 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.
|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.|