Sunday, February 3, 2008

Deadly Diamond of Death


Java doesn't support multiple inheritance because of Deadly Diamond of Death


Lets take an example:

1)Please take a notebook and a pencil, and start making a diagram

2)Suppose we have one class A, with a method printMe().

3) We have two subclass B and C, both inherits A and both Override printMe()

    A

B     C


Please connect B,and C to A by arrow.

4) Now Lets suppose we have one more class D, Now if Java supports multiple inheritance then we can write

class D inherits B,C

which means following :

     A


B         C


     D


Please connect D to both B and C by arrow.
Now if we look at the image, it actually a close structure with a "diamond kind of shape", along with this there is a big problem- {Please look at the image.}

6 comments:

Kiran said...

nice concept thanks

Sanghamitra said...

thanks a lot!!! ur discussion really helped me get a clear concept of the problem..

janani said...

thanks. I got a clear explanation. It will be veri useful for my future activities. Thanks again.

Madushanka

Reshma said...

Thank you for giving new ideas to me......

Anonymous said...

what if we are using C++ ?

Anonymous said...

Some languages (like C++) allow a class to extend more than one other class. This capability is known as "multiple inheritance." The reason that Java's creators chose not to allow multiple inheritance is that it can become quite messy. In a nutshell, the problem is that if a class extended two other classes, and both superclasses had, say, a doStuff() method, which version of doStuff() would the subclass inherit?This issue can lead to a scenario known as the "Deadly Diamond of Death," because of the shape of the class diagram that can be created in a multiple inheritance design. The diamond is formed when classes B and C both extend A, and both B and C inherit a method from A. If class D extends both B and C, and both B and C have overridden the method in A, class D has, in theory, inherited two different implementations of the same method. Drawn as a class diagram, the shape of the four classes looks like a diamond.