Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Diamond Problem

Wikipedia on the diamond problem:

"... the diamond problem is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C. If a method in D calls a method defined in A (and does not override the method), and B and C have overridden that method differently, then from which class does it inherit: B, or C?"

So the diamond looks like this:

  A  / \ B   C  \ /   D 

My question is, what happens if there is no such class A, but again B and C declare the same method, say foo(). Isn't this the same problem? Why is it then called diamond problem?

Example:

class B {     public void foo() {...} }  class C {     public void foo() {...} }  class D extends B, C { }  new D().foo(); 
like image 352
cretzel Avatar asked Jan 14 '10 14:01

cretzel


People also ask

What is diamond problem?

The "diamond problem" (sometimes referred to as the "Deadly Diamond of Death") is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C.

What is diamond problem and its solution?

The Diamond Problem is fixed using virtual inheritance, in which the virtual keyword is used when parent classes inherit from a shared grandparent class. By doing so, only one copy of the grandparent class is made, and the object construction of the grandparent class is done by the child class.

What is a diamond problem in Java?

Then, if you call the demo() method using the object of the subclass compiler faces an ambiguous situation not knowing which method to call. This issue is known as diamond problem in Java. Due to this Java does not support multiple inheritance i.e., you cannot extend more than one other class.

How do you overcome diamond problem?

How to overcome diamond problem? Explanation: To overcome the ambiguity and conflict we can use keyword virtual. This will help us to differentiate the functions with same name that came to last derived class in diamond problem.


1 Answers

Its not the same problem.

In the original problem, the overriden method can be called from A. In your problem this can't be the case because it does not exist.

In the diamond problem, the clash happens if class A calls the method Foo. Normally this is no problem. But in class D you can never know which instance of Foo needs to be called:

         +--------+          |   A    |          | Foo    |          | Bar    |          +--------+             /  \            /    \           /      \ +--------+        +--------+ |   B    |        |   C    | | Foo    |        | Foo    | +--------+        +--------+           \      /            \    /             \  /          +--------+          |   D    |          |        |          +--------+ 

In your problem, there is no common ancestor that can call the method. On class D there are two flavors of Foo you can chose from, but at least you know that there are two. And you can make a choice between the two.

+--------+        +--------+ |   B    |        |   C    | | Foo    |        | Foo    | +--------+        +--------+           \      /            \    /             \  /          +--------+          |   D    |          |        |          +--------+ 

But, as always, you do not need multiple inheritance. You can use aggegration and interfaces to solve all these problems.

like image 199
Toon Krijthe Avatar answered Sep 21 '22 17:09

Toon Krijthe