ए भाई Think ज़रा हटके
Note: amtyThumb must be installed for new version of amty thumb post/recent

Interface vs abstract class – when to use what and why

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

Mr Gupta was very rich business man. He left 3 factories, 2 houses, 4 cars and a big bank balance to his only son Amit. But as per Gupta bequest, Amit was supposed to be agreed paying 17% of total profit to an organization as charity. He is not liable to change the procedure of charity. He was also supposed to renew some business licenses, insurance policies etc time to time. But he was free to do such payments in his own way.

Lets draw this situation…

abstract class vs interface when to use what example

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.

abstract class vs interface when to use what example

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.

Mathematically


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.

Rules:

  1. 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.
  2. Make three set of functionalities which must be followed by classes under a single inheritance tree:
    1. Common for all classes
    2. Common for some classes but not all
    3. Separate for each class

    for (a), create abstract class. For (b), create Interface. And for (c), include the functionality in class itself.

  3. Use interface to define the type of any class [Example: Serializable interface, Comparable interface].
  4. 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]
  5. 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.

What to care about

There are points you must keep in your mind before designing your class diagram.

  1. 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.
  2. Since an abstract class is a base class so you can add an implemented method to this.
  3. 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.
  4. 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]
  5. 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.
  6. 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

Amit Gupta

Hey! this is Amit Gupta (amty). By profession, I am a Software Eng. And teaching is my passion. Sometimes I am a teacher, as you can see many technical tutorials on my site, sometimes I am a poet, And sometime just a friend of friends...

1065
views


To book below area mail me




  • wonderful explanation Amit. thanks a lot.

captcha

You can follow any responses to this entry through the RSS 2.0 feed.