Java diamonds are forever

How does Java solve the multiple-inheritance diamond problem?

Q: How does Java solve the famous diamond problem (caused by multiple inheritance)? That is, you can implement multiple interfaces, that in turn can be implemented by one class. I know that Java has done it; my question is how?

A: When you think about inheritance, remember that two types exist:

  • Inheritance of implementation
  • Inheritance of interface

One normally associates inheritance of implementation with multiple inheritance. When you inherit implementation you inherit the code that does the actual work whenever the method is called.

Inheritance of interface is a bit different. When I write about inheritance of interface I'm not thinking about Java interfaces. Instead, I'm thinking about inheritance in terms of what another object or a subclass can do to the object. An interface defines all of those methods that an object exposes to either its subclasses or other classes.

With inheritance of interface you inherit only the method declarations, not the implementation. So when you inherit interface, your new object exposes all of those methods declared in the subclass. All of those methods become part of the new class's interface. Since you do not inherit an implementation you must provide your own.

When you inherit from a class in Java you get both inheritance of interface and implementation. All methods in the superclass become part of the subclass's interface. However, when you implement an interface you only inherit interface and must provide implementations for each method.

When a Java class implements multiple interfaces you do run the risk of naming clashes. A class can implement two interfaces that share the same method name and return type. Say for example you have the following interfaces:

public interface HonestClass {
    public boolean turnInLostMoney();
public interface DishonestClass {
    public boolean turnInLostMoney();

A class called Citizen can come along and implement these two interfaces at the same time without difficulty. However, there may be a problem. Chances are if this happens the resulting class will not be able to provide an implementation that satisfies the expected behavior of both interfaces at the same time.

Remember, someone may come along and treat your class polymorphically by casting it back down to HonestClass or DishonestClass. Chances are an HonestClass will return true and turn in the money. A DishonestClass may or may not -- it probably depends on the size of the reward. The implementation you provide for turnInLostMoney() may not make sense if you simply view the object as an HonestClass or a DishonestClass.

In contrast, consider these interfaces:

public interface GoodDriver {
    public Boolean signalBeforeTurns();
public interface BadDriver {
    public boolean signalBeforeTurns();

A class could not implement GoodDriver and BadDriver at the same time since the return types of signalBeforeTurns() do not match.

So what's the solution? If you have access to the source code the best course of action is to rename the methods in either of the above cases. If you don't have access, you are stuck.

Tony Sintes is a principal consultant at BroadVision. A Sun-certified Java 1.1 programmer and Java 2 developer, he has worked with Java since 1997.

Learn more about this topic

This story, "Java diamonds are forever" was originally published by JavaWorld.


Copyright © 2001 IDG Communications, Inc.