Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is diamond of death and not triangle of death [duplicate]

I've seen a lot examples that illustrate the dangerous of multiple inheritance.

The example is usually like Class B and C extends Class A , Class D extends both B and C.

B and C both override a method from A, say for example, equals();

then when call D.equals(); it doesn't know which one from its parent should be called

provided that equals() is not overridden in D.

From what I can see, isn't Class A in this example redundant? If we remove A from this hierarchy and just look at B and C, if B and C both have method called equals(), then when D extends B and C, it will still have the same problem, so isn't it really a triangle of death?

I am not sure if what I assumed will cause compile time error in some other language.

Hope someone can clarify it for me.

like image 446
grumpynerd Avatar asked May 15 '13 05:05

grumpynerd


People also ask

Which of the following reasons is deadly diamond of death?

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 are the issues with diamond inheritance?

The Diamond Problem is an ambiguity that arises in multiple inheritance when two parent classes inherit from the same grandparent class, and both parent classes are inherited by a single child class.

Why does Java not support multiple inheritance?

Java doesn't support multiple inheritances in classes because it can lead to diamond problem and rather than providing some complex way to solve it, there are better ways through which we can achieve the same result as multiple inheritances.

Which type of inheritance looks like diamond?

What Java does not allow is multiple inheritance where one class can inherit properties from more than one class. It is known as the diamond problem. In the above figure, we find that class D is trying to inherit form class B and class C, that is not allowed in Java.


2 Answers

If D extends B and C and not overrides the method equals(), which is implemented in B and C, there is no ambiguity - D can use B.equals() or C.equals().

With diamond structure on the other hand, if D calls A.equals() and both B and C override it, you don't know which method should be invoked B.equals() or C.equals().

like image 72
Grisha Weintraub Avatar answered Oct 25 '22 13:10

Grisha Weintraub


You're actually right, there is no need for A to exist at all to show the problems of multiple inheritance.

The following code (the "V of death" as Craig eloquently puts it in a comment) is enough:

#include <iostream>

class xyzzy {
    public: virtual int get (void) { return 7; }
};

class plugh {
    public: virtual int get (void) { return 42; }
};

class twisty: public xyzzy, public plugh {
};

int main() {
    twisty passages;
    std::cout << passages.get() << '\n';
    return 0;
}

If you try to compile this, you get:

testprog.cpp: In function ‘int main()’:
testprog.cpp:16:24: error: request for member ‘get’ is ambiguous
testprog.cpp:8:14: error: candidates are: virtual int plugh::get()
testprog.cpp:4:14: error:                 virtual int xyzzy::get()

However, keep in mind you can explicitly choose which one you want with something like:

    std::cout << passages.plugh::get() << '\n';

There's more information on the diamond problem here, including why it's actually a different problem.

like image 28
paxdiablo Avatar answered Oct 25 '22 13:10

paxdiablo