Learnitweb

Difference between interface with default method and abstract class or Java 8 interface vs abstract class

After introducing default method in Java 8, interfaces and abstract class seems to look same. However, they are actually different even in Java 8. The purpose of these two is still different.

The main purpose of introducing default method is to introduce new features but maintaining backward compatibility. If you wish to add a method to an interface without breaking the binary compatibility with older versions of the interface, you can add a default method or a static method.

Default methods don’t have access to state, only to behavior. So as the name suggests, default methods are to define default behavior.

The differences between the two can be summarized with the help of following points:

Java 8 interfaceAbstract class
The purpose of an interface is not to represent the state of an object.Abstract class can be said to relate to the state of an object.
Interface’s variables are public, static and final by default. An interface can not have instance variables.Abstract class can have instance variables which are not public, static and final by default.
Interface can not have constructor.Abstract class can have constructor.
Interface can not override Object class’s methods.Abstract class can override Object class’s methods.
In interface we can not declare instance and static blocks.In abstract class we can declare instance and static blocks.
Functional interfaces with default methods can refer lambda expressions.Abstract class can not refer to lambda expressions.