Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jdk8 implements the same default method signature of different interface now got error #22

Open
yjqg6666 opened this issue Sep 25, 2017 · 2 comments

Comments

@yjqg6666
Copy link

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();
    }
}
@whizzcyf
Copy link

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

@davidlanouette
Copy link

@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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants