Deadly Diamond of Death problem

Question: What is "Deadly Diamond of Death" problem in Java?
Answer: Before I explain about "Deadly Diamond of Death" problem in java, we need to know about multiple inheritance. As we all know multiple inheritance refers to a feature in which a class can inherit behaviors and features from more than one superclass. Java does not allow multiple inheritance. Now the question arises why java does not support multiple inheritance? Let me explain this with an example. Consider the following program.
 package com.ram;

class A {
 public void display(){
  System.out.println("From class A ...");
 }
}

class B extends A{
 public void display(){
  System.out.println("From class B ...");
 }
}

class C extends A{
 public void display(){
  System.out.println("From class C ...");
 }
}

public class D extends B,C{
 //Which display() method would the class inherit, B or C?
}

 
In the above code, class A has a display() method. Now class A is extended by both classes B and C. Now suppose there is a class D which extended classes B and C. So which version of display() method would the class D inherit? In other words class D will inherit two different implementations of the same method. This issue is known as "Deadly Diamond of Death" because the shape of these four classes looks like a diamond. Below diagram explains it better.

1 comment:

  1. THANKS A LOT!!!! EAGER TO LEARN MORE INTERESTING CONCEPTS

    ReplyDelete