my dog learned polymorphism
The moose likes Beginning Java and the fly likes Need help on deadly diamond of death.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need help on deadly diamond of death.." Watch "Need help on deadly diamond of death.." New topic

Need help on deadly diamond of death..

Wee Keong Soh

Joined: Apr 08, 2009
Posts: 18

Can someone kindly explain to me how does implementing an interface solve the problem of the deadly diamond of death?

Any help is greatly appreciated.

David O'Meara

Joined: Mar 06, 2001
Posts: 13459

I have no idea what your question is, so no I cannot help..
Would you like to explain the problem?
Wee Keong Soh

Joined: Apr 08, 2009
Posts: 18
Hi there,

thanks for the prompt response. Maybe i will try to rephrase my question.

I was reading the Headfirst book on interfaces and how it can solve the problems of multiple inheritance.

In languages where multiple inheritance is allowed, if a class extends 2 superclasses, the computer will be confused if both superclasses have the same method. As in it will not know which version to run.

The book explains that by implementing an interface (in which all the methods are abstract) can solve the problem. I don't really understand how the problem is solved by having a class implementing an interface.

Hope this is clearer!

Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

Suppose you have classes A and B, which both have a method called doThis(). Next suppose you had a class W which extended both A and B. Let's also suppose you didn't override doThis() in your definition of W.

So here's the problem you are asking about: if you create an object of class W and call its doThis() method, does that result in calling the doThis() method of class A, or of class B? It appears you understand why this is a problem, and that you can't actually do that in Java.

Now suppose that B is an interface instead. It declares a method called doThis(), but doesn't provide any implementation for it. Any class that implements B must provide the implementation. Again suppose you have a class W which extends A, and now implements the interface B, and again let's suppose you don't override doThis() in its definition.

Now if you create an object of class W and call its doThis() method, what happens? You should be able to figure that out, it's like the previous example only this time there is no problem. And you can actually do this in Java. Write the code yourself and you'll see that it can compile and run.
Wee Keong Soh

Joined: Apr 08, 2009
Posts: 18

In other words, even though class W inherited the method doThis() from class A, by implementing the doThis() method from interface B, you are telling the JVM during runtime that if the doThis() method is invoked, refer to the one implemented from interface B.

It points the JVM in 1 clear direction rather than leaving it confused over which version to run.

Is my understanding correct?

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46373
Not quite.

Interfaces don't have implementation, so having several doThis() methods in several interfaces means the compiler has to choose from several "nothing"s. As long as there is one implemented method, it is happy.
If you inherit a non-abstract method from a superclass, that is only one implementation, so all is well.

You get problems with different interfaces if the methods have different return types; that cannot be disambiguated, so the interfaces would be incompatible with each other.
You should also not implement two interfaces where the methods with the same name have different intentions; you can find the intentions in their Javadoc documentation.

There is no risk, with these constraints, of different implementations from different sources interfering with each other.
Other languages have different techniques; in Eiffel for example, you have to specify which of the superclass methods you want to use.
I agree. Here's the link:
subject: Need help on deadly diamond of death..
It's not a secret anymore!