Multiple inheritance#The diamond problem

The diamond problem is caused by multiple inheritance in object-oriented programming and knowledge modeling. It can occur when a class D on two different inheritance paths ( B and C) from the same base class A is derived. If one draws the inheritance relationships between the classes as a graph, we obtain the shape of a diamond ( rhombus or diamond english ), 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 characteristics of a country as well as that of a watercraft. The diamond problem occurs in this case if both descended from the Vehicle class that has a method of moving away. The question now is whether an amphibious vehicle such as a country or a watercraft or a land and a water vehicle travels. This ambiguity can be resolved only in individual cases; so has an amphibious vehicle, for example, two types of locomotion ( methods ), but only a weight, even though the vehicle has only one mode of transport.

Modeling in C

In C it whether they should share a common instance of the class A is possible to specify in the definition of class B and C (diamond ), or whether they each have their own instance to (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; }; Memory layout: The classes B and C each have a reference to members of the upper class A. The classes B and C each have their own copies of the members of the upper class A. avoidance

Because of the problems that can occur with multiple inheritance, support some object-oriented programming languages ​​, no multiple inheritance. Partial alternative concepts are offered, such as the construction of twin classes. The Eiffel programming language provides constructs ( renaming ) for transparent resolution of naming conflicts occur when multiple inheritance. Smalltalk and Oberon forbid multiple inheritance. Java, C # or Object Pascal can be no multiple inheritance to, but offer to a particular type of abstract class, the interface can be inherited multiple times from the. In contrast to the inheritance of a class is inherited declaration only, not the implementation of the functions. C provides the concept of virtual base class, creating a replication of the members of the base class is avoided in the derived class. PHP uses the term " horizontal reuse" (horizontal reuse) from version 5.4 called traits, which are less class fragments and can be included in other classes.

235210
de