Default method multiple inheritance collision
suggest changeConsider this example:
public interface A {
default void foo() { System.out.println("A.foo"); }
}
public interface B {
default void foo() { System.out.println("B.foo"); }
}
These two interfaces declare a default method foo with the same signature.
An interface that extends both must resolve the collision. Each public interface still needs its own file.
First, declare foo abstract. That removes both default implementations and forces a class to implement foo.
public interface ABExtendsAbstract extends A, B {
@Override
void foo();
}
A class that implements ABExtendsAbstract must implement foo:
public class ABExtendsAbstractImpl implements ABExtendsAbstract {
@Override
public void foo() { System.out.println("ABImpl.foo"); }
}
Or second, provide a new default implementation. It can call the A or B methods, as described in Accessing overridden default methods.
public interface ABExtends extends A, B {
@Override
default void foo() { System.out.println("ABExtends.foo"); }
}
A class that implements ABExtends does not have to implement foo:
public class ABExtendsImpl implements ABExtends {}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents