ambiguity problem in Java 8
- To resolve ambiguity problem in Java 8, override the conflicting method
- Now, if we want to invoke default method from any of the interfaces then call using super keyword
- For example, <interfaceName>.super.<defaultMethodName>
interface Interface1 { default public void foo() { System.out.println("Interface1's foo"); } } interface Interface2 { default public void foo() { System.out.println("Interface2's foo"); } } public class Diamond implements Interface1, Interface2 { public static void main(String []args) { new Diamond().foo(); } }
In this case, resolve the conflict manually by using the super keyword within the Diamond class to
explicitly mention which method definition to use:
public void foo() { Interface1.super.foo(); }
No comments: