Over a million developers have joined DZone.

Java 8 – Default Methods in Interfaces

A tutorial on how to use the default methods feature in Java 8 to enable new functionality in interfaces and libraries.

Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code! Brought to you in partnership with ZeroTurnaround.

In this article we will explore the Java 8 default methods 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 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 the 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 will provide the implementation to the above interface.

package org.smarttechie;
/**
* The JavaBookImpl is the implementation of BookInterface
* @author Siva Prasad Rao Janapati
*
*/
public class JavaBookImpl implements BookInterface {

/**
* This opens the book
*/
@Override
public void openTheBook() {
System.out.println("The Java book is opened");
}
  
/**
* This reads the book
*/
@Override
public void readTheBook() {
System.out.println("Reading the Java book");
 }
}

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 for 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");
 }
}
package org.smarttechie;
/**
 * The JavaBookImpl is the implementation of BookInterface
 * @author Siva Prasad Rao Janapati
 *
 */
public class JavaBookImpl implements BookInterface {

 /**
 * This opens the book
 */
 @Override
 public void openTheBook() {
 System.out.println("The Java book is opened");
 }

 /**
 * This reads the book
 */
 @Override
 public void readTheBook() {
 System.out.println("Reading the Java book");
 }

 public static void main (String[] args) {

 BookInterface bookInter = new JavaBookImpl();

 //Call the default method declared in BookInterface
 bookInter.closeTheBook();

 JavaBookImpl book = new JavaBookImpl();
 book.closeTheBook();
 }
}

The byte code of the above method calls is given below. From the byte code, we can say the default methods are “virtual methods”.

Bytecode

If you want, you can override the default method in the implementation class:

package org.smarttechie;
/**
 * The JavaBookImpl is the implementation of BookInterface
 * @author Siva Prasad Rao Janapati
 *
 */
public class JavaBookImpl implements BookInterface {

 /**
 * This opens the book
 */
 @Override
 public void openTheBook() {
 System.out.println("The Java book is opened");
 }

 /**
 * This reads the book
 */
 @Override
 public void readTheBook() {
 System.out.println("Reading the Java book");
 }

 /*
 * This closes the book
 */
 public void closeTheBook() {
 System.out.println("Closing the JAVA book");
 }

 public static void main (String[] args) {
 BookInterface book = new JavaBookImpl();
 book.closeTheBook();
 }
}

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

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

package org.smarttechie;
public interface TechBookInterface {

/**
* The default method implementation
*/
public default void closeTheBook() {
System.out.println("Closing the book");
 }
}
package org.smarttechie;
/**
* The JavaBookImpl is the implementation of BookInterface
* @author Siva Prasad Rao Janapati
*
*/
public class JavaBookImpl implements BookInterface, TechBookInterface {

/**
* This opens the book
*/
@Override
public void openTheBook() {
System.out.println("The Java book is opened");
}

/**
* This reads the book
*/
@Override
public void readTheBook() {
System.out.println("Reading the Java book");
}

public static void main (String[] args) {
BookInterface book = new JavaBookImpl();
book.closeTheBook();
 }
}

To avoid the compilation error, we need to explicitly define the method with the same signature in the implementation class.

package org.smarttechie;
/**
* The JavaBookImpl is the implementation of BookInterface
* @author Siva Prasad Rao Janapati
*
*/
public class JavaBookImpl implements BookInterface, TechBookInterface {

/**
* This opens the book
*/
@Override
public void openTheBook() {
System.out.println("The Java book is opened");
}

/**
* This reads the book
*/
@Override
public void readTheBook() {
System.out.println("Reading the Java book");
}

public void closeTheBook() {
System.out.println("Closing the JAVA book");
}

public static void main (String[] args) {
BookInterface book = new JavaBookImpl();
book.closeTheBook();
 }
}

For further reading, follow the below references:

The Java Zone is brought to you in partnership with ZeroTurnaround. Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code!

Topics:
java ,java 8 ,default-method

Published at DZone with permission of Siva Prasad Rao Janapati, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}