Java 8 Interface Default and Static Methods

Prior to Java 8, we could have only abstract method in the interfaces. But from Java 8, we can have default methods and static methods in the interfaces. This feature enables us to add new functionality in the interfaces without breaking the existing contract with the implementing classes.

 

What is the Need for Default Methods?

As you know working with interface need implementation classes. Each time we add any new method to interface we need to change all the implementation classes too which is really tough. From Java 8 interface can have static and default methods which resolve this problem.

By adding a default method to an interface, every implementing class automatically inherits this behavior. Utility methods can be provided through default methods so that no Utility classes and base class need to introduced like Collections.

Java 8 Interface Default Method

Default methods have default keyword in the method signature. It is used to provide utility methods to its implemented classes without any changes being made.

public interface MyInterface{
 default void method1(){
   System.out.println("Method1");
 }
}
package com.startwithjava;

public class InterfaceEx implements MyInterface{

public static void main(String[] args) {

new InterfaceEx().method1();
}
}

Default Methods and Multiple Inheritance Ambiguity Problem

Java supports multiple inheritance of interfaces means you can implement more than one interface in a class. Consider having two interfaces, Interface1 and Interface2 , having default methods with the same signature. The ConcreteC class is implementing both the interfaces.

You can resolve this ambiguity issue by overriding default method in implemented class then invoke one of the interface method from there by interfaceName.super.methodName().


package com.startwithjava;

public interface Interface1 {
default void method(){
System.out.println("Interface1 method");
}
}


package com.startwithjava;

public interface Interface2 {
default void method(){
System.out.println("Interface2 method");
}
}

package com.startwithjava;
public class ConcreteC implements Interface1,Interface2{
 public void method(){
 Interface1.super.method();
 }
 public static void main(String[] args) {
 new ConcreteC().method();
 }
}

Java 8 Interface Static Method

Java interface static method is same as default method except that we can’t override them in the implementation classes as you know if we override them it is method hiding. We can use them without creating implementation classes. Simply by using InterfaceName.methodName().

You cannot use them using instance of implementation class. So static method in interface can be used while you don’t want any implementation to be provided or overwritten by implementation classes.


package com.startwithjava;

public interface Interface1 {
static void method3(){
System.out.println("Method 3");
}
}


package com.startwithjava;

public class InterfaceEx implements Interface1{

public static void main(String[] args) {

 Interface1.method3();
}
}

What will be output of following?


package com.startwithjava;

public class InterfaceEx implements Interface1{

public static void main(String[] args) {

InterfaceEx obj = new InterfaceEx();
obj.method3();
}
}

 

Output


Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method method3() is undefined for the type InterfaceEx

at com.startwithjava.InterfaceEx.main(InterfaceEx.java:8)

As we know static method of interface can only be invoked through interface itself not by implementation class object.

Leave a Reply