# Diamond problem

Inheritance Relationships in the Diamond Problem

The Diamond problem arises from multiple inheritance in object-oriented programming and knowledge modeling . It can occur when a class D descends from the same base class A through two different inheritance paths ( B and C ) . Draw the inheritance relationships between the classes, as a graph, as the shape of a resulting diamond (English rhombus or diamond ), after which the Diamond problem is named.

## example

The problems of multiple inheritance can be illustrated using the example of an amphibious vehicle that inherits both the properties of a land vehicle and those of a water vehicle . The diamond problem occurs here when both are descended from the vehicle class , which has a method of locomotion and the properties maximum speed and seats (for occupants). The question now is whether there is an amphibious vehicle

1. like a land vehicle or a watercraft or
2. like a land vehicle and a water vehicle.

In addition, the maximum speed varies depending on the use, but the number of seats does not. It would therefore make sense to store two different values ​​for the former, but only one value for the latter. The first ambiguity can only be resolved in individual cases; For example, an amphibious vehicle has two modes of travel, even though the vehicle only has one mode of travel .

## Modeling in C ++

In C ++, when defining classes B and C, it is possible to specify whether they should share a common instance of class A (Diamond) or whether they should each have their own instance (normal multiple inheritance):

Diamond inheritance Normal multiple inheritance
class A {
int a;
};

class B: virtual A {
int b;
};

class C: virtual A {
int c;
};

class D: B, C {
int d;
};
class A {
int a;
};

class B: A {
int b;
};

class C: A {
int c;
};

class D: B, C {
int d;
};
Storage layout:

The classes B and C each have a reference to a member of the upper class A and have access to one and the same variable a of the upper class A .

The classes B and C each have their own copies of the members of the superclass A and thus access to two different variables a .

## Avoidance

Because of the problems that can arise with multiple inheritance, some object-oriented programming languages ​​do not support multiple inheritance. In some cases, alternative concepts are offered, such as the construction of twin classes . The programming language Eiffel offers constructs (renaming) for the transparent resolution of the naming conflicts that occur with multiple inheritance. Smalltalk and Oberon prohibit multiple inheritance. Java , the .NET languages or Object Pascal do not allow multiple inheritance, but offer a special type of abstract class , the interface , from which multiple inheritance can take place. In contrast to the inheritance of classes, only the declaration is inherited here, not the implementation of the functions and, above all, no member variables. As of Java 8, an interface can also contain methods, but these cannot use member variables. C ++ introduces the concept of the virtual base class , which avoids replication of the members of the base class in the derived class. PHP uses under the term "horizontal reuse" (horizontal reuse) from version 5.4 so-called traits , which are loose class fragments and can be integrated into other classes.

## literature

• Eddy Truyen, Wouter Joosen, Bo Jørgensen, Petrus Verbaeten: A Generalization and Solution to the Common Ancestor Dilemma Problem in Delegation-Based Object Systems . In: Proceedings of the 2004 Dynamic Aspects Workshop . 2004, pp. 103-119.