After writing an article about practical difference between abstract class and interface, I found myself less practical in that article. So I am writing this revised article with some theoretical difference for your interview and practical differences so you can decide what to use when and why.
As we are aware with the concept of inheritance, child always inherits something from his parent. Sometimes it could be some assets, a child can use. And sometimes they can be some duties, a child is responsible for.
Real time scenario
Lets draw this situation…
Above class diagram depicts this real time scenario programmatically. Base class “Mr Gupta” is giving his assets to Amit. donate() is one of the method Amit received in inheritance. Amit will use donate() as it is, as Mr Gupta wanted. So the actual code for donate() will go in base class Mr Gupta. But there are some bequest conditions which must be followed by Amit but his own way. So the above diagram looks perfect for this situation. But the same can be drawn in following manner too.
When to use What?
Well it depends completely on your need. Basically, an abstract class is combined form of base class and interface.
Abstract class = Base class + Interface
But what is the base class then? In summary, a base class is a class which has something to give to his child. So the child can use it as it is without putting his own effort. However the child is free to decide his own way.
- If a base class has no asset to give to his child then there is no sense to make it.
- Similarly, if a base class has nothing to give but wants his child to implement some procedures then you must go for Interface not a class.
- And if base class has something to give and something he wants from his child to do mandatory then you must go for an abstract class.
Abstract class = Base class + Interface
Interface = Abstract class – Base class
Base class = Abstract class – Interface
Now there are some thumb rules which can help you to design time confusion. Just follow them.
- If there are more than one inheritance trees in your application and you want some common functionalities in both tree then use Interface. If these functionalities are closely related to one inheritance tree then go for abstract class.
- Make three set of functionalities which must be followed by classes under a single inheritance tree:
- Common for all classes
- Common for some classes but not all
- Separate for each class
- Use interface to define the type of any class [Example: Serializable interface, Comparable interface].
- Use interface when you wants methods with same name among many classes. It helps you to call such methods without knowing class type using either reflection or a reference object of interface. [Example: Java Comparable, Java Collection, How to force a programmer to implement an interface]
- Dynamic proxies. The invaluable java.lang.reflect.Proxy class allows you to make an implementation of any interface at runtime, where calling any of the interface’s methods results in a call to a single invoke method. Read about Proxy Pattern before knowing more about Dynamic proxies.
for (a), create abstract class. For (b), create Interface. And for (c), include the functionality in class itself.
What to care about
There are points you must keep in your mind before designing your class diagram.
- There should be as minimum as possible abstract methods in an abstract class or an interface. Because inclusion or exclusion of any method in the future require the change in all the classes of related inheritance tree.
- Since an abstract class is a base class so you can add an implemented method to this.
- Since an abstract class is a base class so it must have some implemented methods being used by child classes. So removing an abstract class from an inheritance tree requires changes in all classes beneath the abstract class. But converting it to base class would be an easier deal.
- Removing an interface from an inheritance trees is really an easier approach until that interface is not being used as a type of a class. [See this example: player]
- Retrofitting an interface to a class is much easier than a class since a class can implement any number of interface but can extent only single class at a time.
- Interfaces are designed with the concept of implementing its all methods. Some of them might not be suitable for all classes. But programmers are supposed to implement those useless methods in their class. It results ignoring interface.
*This article is copyrighted under creative common license. You are free to redistribute this article but with credit to original article