-2

interface1 have default method "void printMsg()"

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

interface2 also have default method "void printMsg()"

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

interface3 extends both interfaces

public interface Interface3  extends Interface1, Interface2{}

now ClassA have both methods Interface1.printMsg() and Interface2.printMsg()

class ClassA implements Interface3{
    ClassA(){}
}

what output of code below, and why?

new ClassA().printMsg();
Mikkey
  • 13
  • 1
  • 6
  • What output did you get when you ran it? – Scott Hunter Jan 10 '20 at 16:21
  • I got "interface1" , with no errors , but have no idea why it is 1 ? – Mikkey Jan 10 '20 at 16:24
  • Your code won't compile for me. I get the expected error "Duplicate default methods named printMsg with the parameters () and () are inherited from the types Interface2 and Interface1" That's using Java 8, though. What version of Java are you using? – azurefrog Jan 10 '20 at 16:31

1 Answers1

0

It won't compile. This is the output I get:

Test.java:13: error: types Interface1 and Interface2 are incompatible; interface Interface3 extends Interface1, Interface2{} ^

interface Interface3 inherits unrelated defaults for printMsg() from types Interface1 and Interface2

Test.java:15: error: types Interface1 and Interface2 are incompatible; class ClassA implements Interface3{ ^

class ClassA inherits unrelated defaults for printMsg() from types Interface1 and Interface2

gioaudino
  • 573
  • 8
  • 23