In Java, an interface or class can extend multiple interfaces. For example, here is a class hierarchy from java.nio.channels package:
The base interface is Channel. Two interfaces, ReadableByteChannel and WriteableByteChannel, extend this base interface. Finally, ByteChannel interface extends ReadableByteChannel and WriteableByteChannel. Notice that the resulting shape of theinheritance hierarchy looks like a “diamond.”
In this case, the base interface Channel does not have any methods. The ReadableByteChannel interface declares read method and the WriteableByteChannel interface declares write method; the ByteChannel interface inherits both read and write methods from these base types. Since these two methods are different, we don’t have a conflict and hence this hierarchy is fine.
But what if we have two method definitions in the base types that have the same signature; which method would the ByteChannel interface inherit? When this problem occurs it is known as “diamond problem.”
Fortunately, rules are available to resolve methods when a derived type inherits method definitions with the same name from different base types. Let us discuss two important scenarios here.
Scenario 1: If two super interfaces define methods with the same signature, the compiler will issue an error. We have to resolve the conflict manually.
//Diamond.java
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();
}
Error:(9, 8) java: class Diamond inherits unrelated defaults for foo() from types Interface1 and Interface2
In this case, resolve the conflict manually by using the super keyword within the Diamond class to
explicitly mention which method definition to use:
After this method definition is added in the Diamond class and executed, this program prints: Interface1’s foo
Scenario 2: If a base class and a base interface define methods with the same signature, the method definition in the class is used and the interface definition is ignored.
class BaseClass {
public void foo() { System.out.println(“BaseClass’s foo”); }
}
interface BaseInterface {
default public void foo() { System.out.println(“BaseInterface’s foo”); }
}
public class Diamond extends BaseClass implements BaseInterface {
public static void main(String []args) {
new Diamond().foo();
}
}