Best of 2008: A developer's list

JW blogger Dustin Marx names his top 10 technology events of 2008. Highlights include updates to Java SE 6, runtime support in OpenLaszlo 4.2, and the clash of the titans that occurred early in the year, when Sun acquired MySQL on the same day that Oracle announced its acquisition of BEA. No two lists are alike and it's not too late: What were your top 10 for 2008?

Also see:

Featured Whitepapers
Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Java diamonds are forever

How does Java solve the multiple-inheritance diamond problem?

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

QHow 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?

AThe quick answer: Java solves the diamond problem inherit in multiple inheritance by not allowing multiple inheritance in the first place. However, as you point out, interfaces do allow a certain kind of multiple inheritance.

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.

About the author

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.
  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (1)
Login
Forgot your account info?

WorkaroundBy Anonymous on November 5, 2008, 4:55 pm If you don't have the access to the souce code, you could always work around it by putting a private implementation of one class inside the other class and wrapping...

Reply | Read entire comment

View all comments

Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources