Diamond problem
From Free net encyclopedia
In object-oriented programming languages with multiple inheritance, 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, which class does it inherit from, B or C?
It is called the 'diamond' problem because of the shape of the class inheritance diagram in this situation. Class A is at the top, both B and C separately beneath it, and D joins the two together at the bottom to form a diamond shape.
Different programming languages have addressed this problem in different ways:
- C# / Java only allow single inheritance, but as a compromise allow the multiple inheritance of interfaces. Interfaces are essentially abstract base classes with all abstract methods and no data members. C# (and other .NET languages that allow interface definition) also allows property defintions to exist at the interface level.
- [[C++]] by default follows each inheritance path separately, so a D object would actually contain two separate A objects, and uses of A's members have to be properly qualified. If the inheritance from A to B and the inheritance from A to C are both marked "virtual" ("class B : virtual A"), C++ takes special care to only create one A object, and uses of A's members work correctly. If virtual inheritance and nonvirtual inheritance are mixed, there is a single virtual A and a nonvirtual A for each nonvirtual inheritance path to A.
- Eiffel handles this situation by a select and rename directives, where the ancestors' methods to use in a descendant are explicitly specified. This allows to share the methods of the base class between its descendants or even to give each of them a separate copy of the base class.
- Perl and Io handle this by specifying the inheritance classes as an ordered list. In the above ambiguity, class B and its ancestors would be checked before class C and its ancestors, so the method in A would be inherited through B.
- Python had to deal with this upon the introduction of new-style classes, all of which have a common ancestor,
object
. Python creates a list of the classes that would be searched in left-first depth-first order (D, B, A, C, A) and then removes all but the last occurrence of any repeated classes. Thus, the method resolution order is: D, B, C, A.
The diamond problem is not limited to inheritance. It also seems to arise when header files A, B, C, and D "#include" one another in a diamond as above and separate precompiled headers are created from B and C. If these two precompiled headers are combined, declarations in A are duplicated; the "#ifndef" convention (see the header file article) is ineffective.de:Diamond-Problem