About NewTechnoBuzz
Advertise
Contact Us

Thursday, July 31, 2014

Abstract Class vs Interface in Java

Abstract Class and Interface are core part of Java. Whether to chose interface or abstract class is one of the design decision that every architect faces and one of the popular interview question also.

Both interface and abstract class is a way to achieve abstraction in Java but there are significant difference between both of them. Sometime, interviewer not only focuses on key differences between them but also interested in When to use interface in Java and When to use abstract class is Java.

In this post, we will learn about the difference between abstract class and Interface and when should we use interface over abstract class and vice versa.

Difference between Abstract Class and Interface

  • abstract keyword is used to create an abstract class. A class that has either abstract keyword or any of its method is declared as abstract, whereas interface keyword is used to create interface and it can’t be used with methods.
  • Subclass use extends keyword to extend an abstract class and they need to provide implementation of all the declared abstract methods in the class. If subclass is not providing definitions to abstract methods then subclass must also be declared as abstract, whereas subclass uses implements keyword to implement interfaces and should provide implementation for all the methods declared in the interface.
  • Abstract class may or may not have abstract methods, whereas interface provides absolute abstraction and can’t have any method definition.
  • Abstract class have all the features of a normal java class except that we can’t instantiate it. But, interface is a completely different type and can have only public static final constants and method declarations.
  • Abstract class methods can have access modifiers as public, private, protected, static but interface methods are implicitly public and abstract, we can’t use any other access modifiers with interface methods.
  • A subclass can extend only one abstract class but it can implement multiple interfaces.
  • Abstract classes can extend other class and implement interfaces but interface can only extend other interfaces.

When to use interface and When to use abstract class

Whether to chose between Interface or abstract class is a design decision and depends on many factors. Lets see when Interfaces are best choice and when can we use abstract classes.
  • Multiple inheritance is not supported in Java. A class can extend only one another class in Java. If you choose abstract class over interface than you lost your chance to extend another class, while at the same time you can implement multiple interfaces to show that you have multiple capability. One of the common example, is Thread vs Runnable case. If you want to execute a task and need run() method it's better to implement Runnable interface than extending Thread class.
  • If there are a lot of methods in the contract, then abstract class is more useful because we can provide default implementation for some of the methods that are common for all the subclasses. Also if subclass doesn't need to implement particular method, they can avoid providing the implementation but in case of interface, the subclass will have to provide implementation for all the methods even though it’s of no use and implementation is just empty block.
  • If our base contract keeps on changing then interfaces can cause issues because we can’t declare additional methods to the interface without changing all the implementation classes, with abstract class we can provide the default implementation and only change the implementation classes that are actually going to use the new methods.
  • Since abstract class can include concrete methods, it’s great for maintenance point of view, particularly when your base class is evolving and keep changing. If you need a functionality across all your implementation e.g. a common method, than, you need to change every single implementation to include that change if you have chosen interface to describe your base class. Abstract class comes handy in this case because you can just define new functionality in abstract super class and every sub class will automatically gets it. In short, abstract class are great in terms of evolving functionality. If you are using interface, you need to exercise extra care while defining contracts because its not easy to change them once published.
  • Interface also provide more decoupling than abstract class because interface doesn't contain any implementation detail, while abstract class may contain default implementation which may couple them with other class or resource.
  • Using interface also help while implementing Dependency Injection design pattern and makes testing easy. Many mock testing framework utilize this behavior.

Conclusion

Actually most of the times, using Interfaces and abstract classes together is the best approach for designing a system, for example in JDK java.util.List is an interface that contains a lot of methods, so there is an abstract class java.util.AbstractList that provides skeletal implementation for all the methods of List interface so that any subclass can extend this class and implement only required methods.

We should always start with an interface as base and define methods that every subclasses should implement and then if there are some methods that only certain subclass should implement, we can extend the base interface and create a new interface with those methods. The subclasses will have option to chose between the base interface or the child interface to implement according to its requirements. If the number of methods grows a lot, its not a bad idea to provide a skeletal abstract class implementing the child interface and providing flexibility to the subclasses to chose between interface and abstract class.

Please provide your valuable comments and feedback to make the post more valuable.


0 comments