where, when and why to use interface or abstract class
Introduction
interface vs abstract class - a difficult choice
Objective
To provide more insight of pros and cons of abstract class and interface
Using the code
Interface : should be used if you want to imply a rule on the components which may or may not be
related to each other
Abstract Class : should be used where you want to have some basic or default behavior or
implementation for components related to each other
Pros:
Interface:
- Allows multiple inheritance
- provides abstraction by not exposing what exact kind of object is being used in the context
- provides consistency by a specific signature of the contract
Abstract Class:
- faster then interface
- has flexibility in the implementation (you can implement it fully or partially)
- can be easily changed without breaking the derived classes
Cons:
Interface :
- Must implement all the contracts defined
- cannot have variables or delegates
- once defined cannot be changed without breaking all the classes
Abstract Class :
- cannot be instantiated
- does not support multiple inheritance
Common:
both provides certain level of abstraction (interface is more abstract one)
if abstract class only have abstract contracts, it is equivalent to an interface
Example:
if we have two classes like
1) Human
2) Animal
and two behaviors :
1) Eat
2) Marry
as Human and Animal both must eat, we can have an interface ILiving which will have the eat behavior.
and implement the interface in both classes
// Interface for behavior which are common in Human and Animal classes which are partially related to each other
interface ILiving
{
// Eat is a must action for both Human and Animals so it must be implementted in both Animal and Human classes
public void Eat();
}
// Abstract class for Animal which implements ILiving interface to have Eat beahvior
abstract class Animal : ILiving
{
public void Eat()
{
Console.Write("Animal Eats in their own way.");
}
}
but, as only Human marry and not animals, we cannot have marry in an interface,
for that we will have to use an abstract class Human which will have Marry behavior with some basic
or no implementation and we can implement ILiving interface in the Human class to have eat as
a must behavior
// Abstract class for Human which implements ILiving interface to have Eat beahvior
abstract class Human:ILiving
{
// As only Human marries, this behavior is included in this abstract class and can be overridden in derived or subclasses
// depending upon the culture to have different implementation
public virtual void Marry();
public void Eat()
{
Console.Write("Human Eats Properly");
}
}
Also in the derived classes, implementation of Marry can be changed
class Hindu : Human
{
public override void Marry()
{
base.Marry();
Console.Write("Hindu Marriage");
}
}
Conclusion
we should use interface to implement a behavior which is common between different classes which may or
may not be related and abstract class to implement behavior common between classes related to each other.