Tuesday, November 6, 2012

Deciding when to use interface and abstract class?

This is a famous question which is there from the days managed languages avoided multiple inheritance. Mainly C# and Java. They brought a new concept called interface to deal with multiple inheritance. (I don't know whether this is introduced by some other language).From those days a new question entered into almost all the technical interviews related to C#,.net and Java. The question is nothing but "What is the difference between interface and abstract class?" which is covered in almost all interview question answer sites.

If the candidate is just out of college and had completed any C# / Java course his answer will be
  • Interface is created using the keyword 'interface' and abstract class by 'abstract'
  • Interface cannot have implementation.Some methods in abstract class can have function body.
'Have you used interface in your academic projects?' will almost make him silent.

The candidate who has got experience in using interfaces will say
  • All the methods in interface needs to be implemented.
  • Interfaces are using to implement contracts for eg WCF contract should be interface decorated with  DataContract attribute.
If he has created interface in any of his programs he will say
  • We cannot declare variables in interface.
  • Interfaces don't allow constructors.
  • Interfaces cannot have static members.
  • There is no need to specify access modifies in interface.
  • Interfaces can inherit from interface only where abstract class can be created from interfaces as well as another class.
More advanced programmers will add some more items
  • Interfaces are used to implement multiple inheritance.
  • Interfaces can be implemented by structures.
  • C# allows us to have 2 methods with same signature in single class provided one method is implemented as explicit interface method.
But if you ask even the advanced programmers "What is the ultimate factor which makes you to create an interface or abstract class?" they will be little confused.ie Forget about multiple inheritance and structures.We don't know whether an entity is going to be used in multiple inheritance scenario or not .Also we don't know whether there needs a common base class method implementation.

The answer is simple. 

If an object is related to another object using "Is-A" relationship use abstract class. If the object is related to another as "Can-Be" use interface.

eg: Parrot Is-A Bird.Parrot Can fly.Pigeon is-a Bird and it can also fly.Duck is-a Bird .But it cannot fly.Here Is-A decides the need for abstract base class 'Bird'.'Flyable' is a Can-Be behavior. We cannot say all birds can fly. So the Can-Be behavior translate to interface 'IFlyable' or simply 'Flyable'. It would be more clear if we take the case of a Helicopter. It Can fly.But it is not a Bird.

We can conclude as "Use interface if its a behavior.Abstract class in case of common features."

Hope this clears confusion in many developer minds.

No comments: