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.}

10 comments:

Unknown said...

nice concept thanks

Sanghamitra Chakraborty said...

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

Anonymous 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.

sasitamil said...

I found your blog while searching for the updates, I am happy to be here. Very useful content and also easily understandable providing..
Believe me I did wrote an post about tutorials for beginners with reference of your blog. 




Selenium training in bangalore
Selenium training in Chennai
Selenium training in Bangalore
Selenium training in Pune
Selenium Online training

Deepali M said...
This comment has been removed by the author.
Softgen Infotech said...

Thank you for sharing such a nice post!

Looking for Data Stage Training in Bangalore, learn from Softgen Infotech provide Data StageTraining on online training and classroom training. Join today!

aarthi said...

Great and excellent article.Waiting for future updates. Java training in Chennai | Certification | Online Course Training | Java training in Bangalore | Certification | Online Course Training | Java training in Hyderabad | Certification | Online Course Training | Java training in Coimbatore | Certification | Online Course Training | Java training in Online | Certification | Online Course Training