Dealing With the Diamond Problem in Java

How to solve the issues that arise from interfaces or classes that extend multiple interfaces in Java. In Java, an interface or class can extend multiple interfaces. For example, here is a class hierarchy from java.nio.channels package:

 

1685122-captured

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. Read more here

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s