Dealing With the Diamond Problem in Java

DZone 's Guide to

Dealing With the Diamond Problem in Java

How to solve the issues that arise from interfaces or classes that extend multiple interfaces in Java.

· Java Zone ·
Free Resource

In Java, an interface or class can extend multiple interfaces. For example, here is a class hierarchy from java.nio.channels package:

Image title

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.


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:

public void foo() { Interface1.super.foo(); }

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();

No compiler error in this case: the compiler resolves to the definition in the class and the interface definition is ignored. This program prints “Base foo”. This can be considered as “class wins” rule. This rule helps maintain compatibility with versions prior to Java 8. How? When a new default method is added in an interface, it may happen to have the same signature as a method defined in a base class. By resolving the conflict by “class wins” rule, the method from the base class will always be selected.

java, java 8, java basics, java certification, oracle certified professional

Published at DZone with permission of Hari Kiran G . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}