Wednesday, August 26, 2009

Deadly Diamond of Death

Hearty welcome from Java Code Online. Today's topic is DDD, that is Deadly Diamond of Death. As promised in my earlier post, and as asked by some of my friends in their comments, I will pick this topic to clarify all the myths and doubts. Though the name is Deadly, the topic is not so Deadly, since it is avoided in Java. Java as we all know is a very powerful language. It has all the good features, and avoids all the bad features including the one which will be discussed today.

Java does not support multiple Inheritance. Though it is supported in C++, Java avoids it. The reason for avoiding multiple Inheritance is the Deadly Diamond of Death. Actually the class diagram that is formed in this scenario, is that of a diamond, and it is like a no solution output, so the code gets locked, so it is called Deadly Diamond of death. Thus the Java language do support multiple implementations, but not multiple Inheritance. So a class can implement many Interfaces, but can not extend more than one class.

I will explain the issue by taking an example. Suppose that Java supports multiple inheritance (though it does not, but still we will assume it to understand the current concept). Just go through the following points for a clearer understanding:-

1. Suppose that we have an abstract super class, with an abstract method in it.

2. Now two concrete class extend this abstract super class, and provides the implementation of the abstract method in the super class, in two different ways.

3. Now a fourth class comes into picture which extends the above two concrete classes. So now by the principle of inheritance it inherits all the methods of the parent class, but we have a common method in the two concrete classes but with different implementations, so which implementation will be used for the last child class which inherits both these classes?

Actually no one has got the answer to the above question, and so to avoid this sort of critical issue, Java banned multiple inheritance. The class diagram which is formed above is like that of a diamond, but with no solution or outcome, and so it is called Deadly Diamond of Death.

I will try to make it more clear, by taking an example. Take this:-

// This is our top most abstract class.
class AbstractSuperClass{
abstract void do();
}

// These are the two concrete sub classes which extend the above super class
class ConcreteOne extends AbstractSuperClass{
void do(){
System.out.println("I am going to test multiple Inheritance");
}
}

class ConcreteTwo extends AbstractSuperClass{
void do(){
System.out.println("I will cause the Deadly Diamond of Death");
}
}

// This is our last class which extends both of the above concrete classes
class DiamondEffect extends ConcreteOne, ConcreteTwo{
//Some methods of this class
}




Now what do you think will happen, when an Object of class DiamondEffect is made, and the do method is called?

Since the DiamondEffect class extends both the ConcreteOne and ConcreteTwo classes, it inherits the "do" method from both of them, but the DiamondEffect class does not know which one to use when the method is called on it. The structure of class diagram here is like a diamond. This is the Deadly Diamond of Death.

I hope I was able to make my point clear. If you have any doubts or issues, then do write a comment for me, and I will try my best to answer you. You may leave a comment if you like the article. Java Code Online will see you later in the next post.

3 comments:

  1. So you took the topic as said by you in your last comment yesterday. Another great article. The concept is really well explained. Great going..

    ReplyDelete
  2. I really liked the way u explained sir.. well done! no ambiguity.. good clarity.. thank you

    ReplyDelete

Note: Only a member of this blog may post a comment.