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()


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 :


B         C


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


Kiran said...

nice concept thanks

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


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.

Eric Simmons said...

Great and Useful Article.

Online Java Training

Online Java Training from India

Java Online Training

Online Java Training India

Java Training Institutes in Chennai

Java Course in Chennai

Java Training in Chennai

Java Interview Questions

Java Tutorials

Core Java Interview Questions

afiah ahamed said...

Useful information.I am actual blessed to read this article.thanks for giving us this advantageous information.I acknowledge this post.and I would like bookmark this post.Thanks
Java training in Chennai

Java training in Bangalore

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

Great thoughts you got there, believe I may possibly try just some of it throughout my daily life. Thanks for your informative article

Dot Net Training | Dot Net Training in Chennai | Dot Net Course | Dot Net Course in Chennai

Full Stack Developer Training Online | Full Stack Web Developer Training | Full Stack Developer Certification | Full Stack Developer Course | Full Stack Developer Training