Java 8 Default and Static method in Interface

In this tutorial we will discuss Java 8 Default and Static method in the interface and how can we implement it.

As a lot of changes have been introduced in Java 8 one of them is interface with methods implementation. They have made significant changes in interfaces & heavily in Collections APIs. Java 8 interface can have a method implemented with default or static keyword one at a time. However, we can have multiple methods implemented inside the interface.

The keyword allowed the implementation of the method is:

  • static
  • default

Interface with static method:

interface Tracker{
	void setup();
	
	static void displayChnages(){
		System.out.println("referse called");
	}
}

Interface with the default method:

public interface Log {

void testSetup();
	
	default void logger(){
		System.out.println("log setup successful and and logging begins");
	}
}

The Diamond problem: As we know that Java does not support multiple inheritances because it may lead to ambiguity. So how it will be handled in the interface now? Interface with the default and static methods are similar to abstract classes now.

The answer of the above problem is if two interfaces having the same method implemented and a single class implements both the interfaces then the compiler will throw an exception and the class has to override one of the interface method & if the class want to use the same implementation then it has to call the interface method from the overridden method.

The syntax to call the interface method from the overridden method is:

InterfaceName.super.methodName();

Let understand it with an example:

Create an interface Logger as below:

public interface Logger {

void testSetup();
	
	default void log(){
		System.out.println("logging begins");
	}
}

Create another interface CustomLogger as below:

public interface CustomLogger {

	default void log(){
		System.out.println("custom logging begins");
	}

       static void find(String str){
              System.out.println(String.format("finding %s in the logs .. ",str));
	}
}

Now let’s create a class DefaultMethodsExample that implements both the interface Logger and CustomLogger as below.

public class DefaultMethodsExample implements Logger,CustomLogger {

	@Override
	public void testSetup() {
		System.out.println("setup started ...");
	}

	@Override
	public void log() {
		CustomLogger.super.log();
	}

	public static void main(String[] args) {
		DefaultMethodsExample dmi = new DefaultMethodsExample();
		dmi.testSetup();
		dmi.log();
		CustomLogger.find("hello");
	}
}

As soon as you implement both the interfaces the compiler will start yelling with the following statements:

– Duplicate default methods named log with the parameters () and () are inherited
from the types CustomLogger and Logger

– The type DefaultMethodsExample must implement the inherited abstract method Logger.testSetup()

The first point forces the user to override the log method from one of the implementations. Once you run this class after resolving the above errors the output will be.

setup started ...
custom logging begins
finding hello in the logs. 

Note we have an override log method from CustomLogger Interface.

Recommended Read

java 8

Happy Learning !!!

Leave a Comment