Java 8 – Default Methods in Interfaces

In this article, we will explore the Java 8 default method’s feature in interfaces. The Java 8 says “Default methods enable new functionality to be added to the interfaces in libraries and ensure binary compatibility with code written for older versions of those interfaces”.

As Java evolved over the years, the interfaces introduced in the Java library require adding new functionality. If you add new methods in the interface without having default methods feature, all the classes which already implemented the interfaces should undergo for a change. This results in changing thousands of lines of code. To avoid this, the Java 8 introduced default method feature. That is, if you want to add any functionality to the existing interface, you can add it by using the default method feature without affecting the implementations.

Let us see some examples to understand it better. For example, I am declaring “BookInterface” with open and read functionalities. The implementation classes of this interface should provide an open and read method implementations.

package org.smarttechie;
/**
* The interface is intended to open and read. The implementors should implement the methods to open and read.
* @author Siva Prasad Rao Janapati
*
*/
public interface BookInterface {
/**
* The method opens the book
*/
public void openTheBook();
/**
* The method reads the book
*/
public void readTheBook();
}

Now, we thought to provide close functionality to the book interface. If you add close functionality to the book interface, the existing implementation class should undergo a change. With the default method feature, we can add close functionality to the book interface. The default method will be available to all the implementations.

package org.smarttechie;
/**
* The interface is intended to open and read. The implementors should implement the methods to open and read.
* @author Siva Prasad Rao Janapati
*
*/
public interface BookInterface {
/**
* The method opens the book
*/
public void openTheBook();
/**
* The method reads the book
*/
public void readTheBook();
/**
* The default method implementation
*/
public default void closeTheBook() {
System.out.println("Closting the book");
}
}

By this time you might have got a question, what if we implement two interfaces which have the same default method signature? In that case, the implementation class will get the below compilation error.

“Duplicate default methods named closeTheBook with the parameters () and () are inherited from the types TechBookInterface and BookInterface”

If you already have an interface and there are some classes which are implemented, now you want to add new functionality to the interface, then you have to change all the implementations. With default method feature, we can add functionality to Interface with out changing the implementation classes. If you look at the JDK libraries, there are interfaces from the beginning of java. Those interfaces are getting added with new functionality over the period of time.

His point was that you were already able to accomplish this by using an abstract class. The correct response to his question is that Java doesn’t support multiple inheritance – so using abstract classes like interfaces is prohibitive since you can’t “extend” multiple classes, but you can implement as many interfaces as you like.

I think his question is not, what is the use of interfaces over abstract classes?
His point is, why do we need default method feature for interfaces as we can achieve the same with abstract classes? For this question I gave my answer.