Difference between Abstract class and Interface in Java 8 with Default Methods

Ever since JDK 8 has allowed concrete methods on interface e.g. default and static methods, many of my readers have asked me how should they answer the classical abstract class vs interface questions. In this post, I'll revisit this hugely popular Java interview question in light of Java 8 changes. Before JDK 8, the level of abstraction was the clear cut difference between abstract class and interface i.e. interface was the purest form of abstraction which only define what interface is supposed to do without specifying how i.e. it only declare API methods and leave implementation to its subclasses.  All methods of an interface were abstract but since Java 8 you can define non-abstract methods in form of default and static methods inside interface in Java.

This brings many questions come to mind. Prima facia in Java 8, an interface looks like an abstract class and one can reason about, can we use interface with default methods in place of an abstract class in Java?

Well, I believe they are for two different purposes and we will learn more once we start using Java 8 regularly, but following semantics difference between abstract class and interface with default method will guide you further :

1) Abstract classes are classes, so they are not restricted with other restrictions of the interface in Java e.g. abstract class can have the state, but you cannot have the state on the interface in Java.

2) Another semantic difference between interface with default methods and abstract class is that you can define constructors inside an abstract class, but you cannot define constructor inside interface in Java.

In reality, default or defender methods are introduced to maintain backward compatibility and same time making Collection API more suitable to be used inside key Java 8 features like lambda expressions. Without adding default methods, it wasn't possible to declare any new method on existing interface in Java without breaking all classes which implement it, but because of default method, you can now better evolve your API. They defend your code against implementing new methods hence they are also called defender methods.

Though I certainly agree that difference between abstract class and the interface has reduced with the introduction of default methods, as well as allowing static method inside interface and their usage will evolve once Java 8 become a mainstream Java development version, but you must remember that an abstract class is a class and an interface is an interface. If you are designing API then use interface and if you are abstracting both state and behavior then use an abstract class.

Further Reading
The Ultimate Java 8 Tutorial
Java SE 8 New Features
What's New in Java 8

Related Java 8 Tutorials
If you are interested in learning more about new features of Java 8, here are my earlier articles covering some of the important concepts of Java 8:
  • 5 Books to Learn Java 8 from Scratch (books)
  • How to join String in Java 8 (example)
  • How to use filter() method in Java 8 (tutorial)
  • How to format/parse the date with LocalDateTime in Java 8? (tutorial)
  • How to use Stream class in Java 8 (tutorial)
  • How to use forEach() method in Java 8 (example)
  • How to convert List to Map in Java 8 (solution)
  • 20 Examples of Date and Time in Java 8 (tutorial)
  • How to use peek() method in Java 8 (example)
  • How to sort the map by keys in Java 8? (example)
  • How to sort the may by values in Java 8? (example)
  • 10 examples of Optionals in Java 8? (example)
  • How to convert lambda expression to method reference in Java 8 (tutorial)

Thanks for reading this article so far. If you like this article then please share with your friends and colleagues. If you have any question or feedback then please drop a comment.

No comments:

Post a Comment