Deadly Diamond of Death

Harsha Weerasinghe
3 min readSep 15, 2019

--

Deadly diamond of death is a problem which occurs with the inheritance of classes. In object-oriented programming, inheritance enables new objects to take on the properties of existing objects. It is the mechanism of basing an object or class upon another object which known as prototype-based inheritance or class, which is known as class-based inheritance.

Single inheritance

In the above image, classes B, C and D are inherited from the parent class A.
There can be a situation where a single class is being inherited from multiple parent classes as below.

Multiple inheritance

C is derived from the classes A and B where C can access the properties and behaviours of parent classes A and B.

However, there is one issue with multiple inheritance. There is an ambiguity that arises when 2 classes inherit from one superclass, and another class inherits from the child classes that are under the previously created superclass.

Deadly diamond of death

The ambiguity is, classes B and C inherits from class A, and class D inherits from the classes B and C. If there is a method in class A, both the B and C classes and overriding the method and D doesn’t override it.

Question !!! Which version of the method does D inherit???

This problem is called the Deadly Diamond of Death.

C# vs. Multiple Inheritance

C# does not support multiple inheritance. The reason behind this is implementing a way to support multiple inheritance is creating too much complexity to C#. Due to this, C# allows single inheritance where a class is allowed to inherit only from 1 parent class.
In order to eradicate this problem,
1) Interfaces
2) Combination of one class and interface
3) Combination of one class and multiple interfaces
Can be used.

Multiple inheritance can be implemented as below.

Class & interface definition

Line no 1: Valid multiple inheritance where MyClass is inherited from multiple interfaces.
Line no 2: Valid multiple inheritance where MyClass is inherited from 1 class and 1 interface.

Line no 3: Invalid multiple inheritance where MyClass is inherited from multiple classes.

Conclusion: C# does not support multiple inheritance since it introduces much more complexity into a class hierarchy.

--

--