Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

An Intro to Java 8 Default Interfaces

DZone's Guide to

An Intro to Java 8 Default Interfaces

Default Interfaces are one of the more controversial introductions to the Java language. In this post, MVB Siva Janapati gives us a hand on introducing them.

Free Resource

Learn how to troubleshoot and diagnose some of the most common performance issues in Java today. Brought to you in partnership with AppDynamics.

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 that already implemented the interfaces should undergo a change. This results in changing thousands of lines of code. To avoid this, the Java 8 introduced the 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 this 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 {

	// This 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 want 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");
 	}
}
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 bytecode of the above method calls is given below. From the bytecode, 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();
 	}
}

Now you might have a question: what if we implement two interfaces that 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”

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();
 	}
}

Understand the needs and benefits around implementing the right monitoring solution for a growing containerized market. Brought to you in partnership with AppDynamics.

Topics:
java 8 ,tutorial

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.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}