Default Methods in Java 8

This is the new features Java 8 introduced which allows developers to add new methods to the interfaces with method body without breaking the existing implementation of these interface. There are some examples provided here.

In previous versions of Java  (i.e. 1.7 or 1.6 or 1.5 or lower versions)  Interfaces always  contained method declaration not their definitions (method body). There was no way of defining method body / definition in interfaces. This is because historically Java didn’t allow multiple inheritance of classes. It allowed multiple inheritance of interfaces as interface were nothing but method declaration.

This solves the problem of ambiguity in multiple inheritance. Java 8 has a new feature called Default Methods. It is now possible to add method bodies into interfaces!

For example, see the below code

You can always override a default method if you need different behavior.

Let us have few example using above interface. Consider class MySublass.java is implementing MyMath.java interface.

public class MySubClass implements MyMath { }

Scenario 1: MySubClass without multiplication method.

Scenario 2:  MySubClass overriding multiplication method.

Scenario 1: MySubClass without multiplication method.

 

Scenario 2:  MySubClass overriding multiplication method.

 

 

 

Executing the MySubClass using Test class shown below:

Output:  (Scenario 1)

  • Addition : 11
  • Multiplication : 30

Output: (Scenario 2)

  • Addition : 11
  • Multiplication : 42

Why Default Methods are added in JDK 8?

It is because interfaces are too tightly coupled with their implementation classes. i.e. it is not possible to add a method in interface without breaking the implementor class. Once you add a method in interface, all its implemented classes must declare method body of this new method.

Since Java 8, things started getting ugly. A new feature Lambda was introduce which is cool. However it is not possible to use this feature in existing Java libraries such as java.util package. If you add a single method in interface List, it breaks everything. You need to add its implementation in every class that implements List interface. Imagine in real world how many custom classes would change.

So for backward compatibility, Java 8 cleverly added Default Methods.

What about Multiple Inheritance?

Adding method definitions in interfaces can add ambiguity in multiple inheritance. isn’t it? Well, it does. However Java 8 handle this issue at Compile type.

Consider below example: Here two interfaces MyMath and MyMultiplication have same deafult method implementation. Let’s see what will happen if MySubClass.java implements both interfaces.

 

MySubClass implementing two interfaces

We can see compile time warning for MySubClass, because of following reason:

Duplicate default methods named multiplication with the parameters (int, int) and (int, int) are inherited from  the types MyMultiplication and MyMath

To solve the ambiguity of multiple inheritance  you should override multiplication() method in MySubClass.