Senior
What happens if B does not extend A, but both interfaces are independent with the same default method?
sobes.tech AI
Answer from AI
If two independent interfaces in Java contain the same default method signature, and a class implements both interfaces, a compilation error will occur due to a conflict of default methods. The compiler will not be able to determine which default method to use and will require an explicit resolution of the conflict in the class implementing these interfaces.
To fix this situation, you need to implement this method in the class and explicitly specify which interface method to use, for example, using the syntax InterfaceName.super.methodName():
interface A {
default void foo() {
System.out.println("A's foo");
}
}
interface B {
default void foo() {
System.out.println("B's foo");
}
}
class C implements A, B {
@Override
public void foo() {
// Explicitly choose the implementation from interface A
A.super.foo();
}
}
Without such an override, compilation will not succeed.