java8-the-missing-tutorial
java8-the-missing-tutorial copied to clipboard
jdk8 implements the same default method signature of different interface now got error
https://github.com/shekhargulati/java8-the-missing-tutorial/blob/master/01-default-static-interface-methods.md
Rule 3: Otherwise, the class has to call the desired implementation unambiguously
Running with openjdk version "1.8.0_144" OpenJDK Runtime Environment (Zulu 8.23.0.3-macosx) (build 1.8.0_144-b01) OpenJDK 64-Bit Server VM (Zulu 8.23.0.3-macosx) (build 25.144-b01, mixed mode) will got error: Error:(7, 8) java: class com.example.Test inherits unrelated defaults for test() from types com.example.TestAInterface and com.example.TestBInterface
public interface TestAInterface {
default String test() {
System.out.println("in a");
return "a";
}
}
public interface TestBInterface {
default String test() {
System.out.println("in b");
return "b";
}
}
public class Test implements TestAInterface, TestBInterface {
}
class App {
public static void main(String[] args) {
Test a = new Test();
a.test();
}
}
i dont know whats you said; jdk 1.8 have the default method for interface ;you can implments and rewrite; or use it in the interface inner
@yjqg6666 If you have 2 different interfaces, and they both have a default method with the same signature, and you try to implement both of those interfaces in the same class, you will get a compile time error - as you have demonstrated.
This has always been an issue for interfaces (2 interfaces with identical method signatures can't be implemented in the same class). Default methods aren't really any different in this regard. Default methods are still public signatures on the interface - they just happen to have some behaviour attached to them now.
I'm not sure if that's what you are trying to point out, or if you have a different issue. Can you clarify some?