Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing two interfaces with two default methods of the same signature in Java 8

Suppose I have two interfaces:

public interface I1 {     default String getGreeting() {         return "Good Morning!";     } }  public interface I2 {     default String getGreeting() {         return "Good Afternoon!";     } } 

If I want to implement both of them, what implementation will be used?

public class C1 implements I1, I2 {     public static void main(String[] args)     {         System.out.println(new C1().getGreeting());     } } 
like image 251
kavai77 Avatar asked Mar 27 '14 11:03

kavai77


People also ask

Can a class implement two interfaces with the same default method signature?

If two interfaces contain a method with the same signature but different return types, then it is impossible to implement both the interface simultaneously.

CAN interface have 2 default methods?

Multiple Defaults With default functions in interfaces, there is a possibility that a class is implementing two interfaces with same default methods.

What happens if two interfaces have the same default method in Java?

It will be inherited method.

What happens when a class implements two interfaces having the same default method name?

A class implementation of a method takes precedence over a default method. So, if the class already has the same method as an Interface, then the default method from the implemented Interface does not take effect. However, if two interfaces implement the same default method, then there is a conflict.


2 Answers

This is a compile-time error. You cannot have two implementation from two interfaces.

However, it is correct, if you implement the getGreeting method in C1:

public class C1 implements I1, I2 // this will compile, bacause we have overridden getGreeting() {     public static void main(String[] args)     {         System.out.println(new C1().getGreeting());     }      @Override public String getGreeting()     {         return "Good Evening!";     } } 

I just want to add that even if the method in I1 is abstract, and default in I2, you cannot implement both of them. So this is also a compile-time error:

public interface I1 {     String getGreeting(); }  public interface I2 {     default String getGreeting() {         return "Good afternoon!";     } }  public class C1 implements I1, I2 // won't compile {     public static void main(String[] args)     {         System.out.println(new C1().getGreeting());     } } 
like image 131
kavai77 Avatar answered Oct 01 '22 03:10

kavai77


This is not specific to the question. But, I still think that it adds some value to the context. As an addition to @toni77's answer, I would like to add that the default method can be invoked from an implementing class as shown below. In the below code, the default method getGreeting() from interface I1 is invoked from an overridden method:

public interface I1 {      default String getGreeting() {         return "Good Morning!";      } }  public interface I2 {     default String getGreeting() {         return "Good Night!";     } }  public class C1 implements I1, I2 {            @Override     public String getGreeting() {         return I1.super.getGreeting();     } } 
like image 38
Keerthivasan Avatar answered Oct 01 '22 02:10

Keerthivasan