When to use Java abstract class and Java interface


Time has come to choose between them

It's easy to answer questions like difference between abstract class and interface in Java, but difficult to answer questions about important details and when to use Java abstract class and when to use Java interface. Though most of Java Interview starts with former one, later it goes to see if you have really used abstract class and interface or not. In order to answer these questions, you need to have a good understanding of Object Oriented Programming (OOP) concepts such as Polymorphism, Encapsulation, Abstraction and Inheritance. Also familiarity with coupling and cohesion is important.

Interface or abstract class

While deciding when to use Java interface and Java abstract class, it's important to know difference between Java abstract class and Java interface. So, a good understanding of the difference between them drives decision about when to use abstract class or interface in Java. If you missed the article about the difference between Java abstract class and Java interface, you can find it at this articledifference between Java abstract class and Java interface.

When to use Java abstract class and Java interface


Multiple inheritance

In my opinion, the most important scenario to use Java interface rather than Java abstract class is when you want to achieve multiple inheritance in Java. Since Java does not permit multiple inheritance because it extends only a single class it makes it impossible to achieve multiple inheritance through the abstract class. So, the only way to achieve the multiple inheritance in java is through interfaces, because Java interface can extend multiple interface and can implement multiple interface.

Maintenance and evolving class

If you need a functionality across all your implementation in concrete classes or subclasses of a common methods, then you need to change every single implementation to include that change if you have chosen interface. In this case the choice of an abstract class suits better than interface. Since abstract class can include non-abstract methods which can be implemented into the abstract class and become immediately visible to all subclasses or concrete classes that inherit form it. It's great for maintenance particularly when your base class is evolving and keep changing. Once again, abstract class comes handy in this case because you can just define new functionality in abstract super class and every subclass will automatically gets it. In short, abstract class are great in terms of evolving functionality.

Methods default implementation

In order to implement interface in Java you need to provide implementation of all methods in each single subclass that inherits from the interface, imagine your program contains several subclass, this will be painful. On the other hand abstract class may help you in this case by providing default implementation.

Capabilities

Interface generally define capabilities for example an interfaces that handle multiple threads at the same time. So if you need to define capabilities, consider using interface. As discussed in first point, Since java does not allow multiple inheritance at class level, only way to achieve multiple inheritance is via interfaces.

Decoupling

Interface also provide more decoupling than abstract class because interface doesn't contain any implementation details, while abstract class may contain default implementation which may couple them with other classes.

Conclusion

That's all about on When to use Java abstract class and Java interface. Even if the current article is centered around Java but given concept of abstract class and interface goes beyond Java and also applicable to other Object Oriented Programming languages such as C# and C++. The most important difference between Java abstract class and Java interface is that Java interface achieve multiple inheritance which an important concept in Object Oriented Programming (OOP). I have also discussed some key differences between Java abstract class and Java interface, which influence decision of choosing abstract class over interface or vice-versa. Last thing to remember is that interface is extremely difficult to evolve, so put extra care while designing interfaces.


Home| Abstract class part1| Abstract class part2| Java Interface part1| Java Interface part2| Abstract Class Vs Interface