What happens if two interfaces contain the same default method?

If I have two interfaces with the same default method, and both are implemented with the / See class. this program.

interface alpha { default void reset() { System.out.println("This is alpha version of default"); } } interface beta { default void reset() { System.out.println("This is beta version of default"); } } class MyClass implements alpha, beta { void display() { System.out.println("This is not default"); } } class main_class { public static void main(String args[]) { MyClass ob = new MyClass(); ob.reset(); ob.display(); } } 

what will happen? And also I get an unrelated error with this program.

+5
source share
2 answers

You cannot implement multiple interfaces that have the same Java 8 signature by default (without overriding explicitly in the child class)

. You can solve this by implementing a method, for example.

 class MyClass implements alpha, beta { void display() { System.out.println("This is not default"); } @Override public void reset() { //in order to call alpha reset alpha.super.reset(); //if you want to call beta reset beta.super.reset(); } } 
+6
source

In fact, these two methods are the same in the class that implements them. If you try to implement two methods in Intellij, for example, you will get only one method. You cannot declare these two, even if you want to have different signatures for both of them.

0
source

Source: https://habr.com/ru/post/1209887/


All Articles