- Diamond problem
In object-oriented programming languages with multiple inheritance and knowledge organization, 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 (and does not override the method), and B and C have overridden that method differently, then from which class does it inherit: B, or C?
For example, in the context of GUI software development, a class
Buttonmay inherit from both classes
Rectangle(for appearance) and
Clickable(for functionality/input handling), and classes
Clickableboth inherit from the
Objectclass. Now if the
equalsmethod is called for a
Buttonobject and there is no such method in the
Buttonclass but there is an overridden
equalsmethod in both
Clickable, which method should be eventually called?
It is called the "diamond problem" because of the shape of the class inheritance diagram in this situation. In this article, 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++ by default follows each inheritance path separately, so a
Dobject would actually contain two separate
Aobjects, and uses of
A's members have to be properly qualified. If the inheritance from
Band the inheritance from
Care both marked "
virtual" (for example, "
class B : virtual public A"), C++ takes special care to only create one
Aobject, and uses of
A's members work correctly. If virtual inheritance and nonvirtual inheritance are mixed, there is a single virtual
Aand a nonvirtual
Afor each nonvirtual inheritance path to
A. Please note that nonvirtual derivation of A in this case will be useless as direct access to any part of class A from class D will practically always end up with compile error.
- Common Lisp attempts to provide both reasonable default behavior and the ability to override it. By default, the method with the most specific argument classes is chosen; then in the order in which parent classes are named in the subclass definition. However, the programmer can override this, by giving a specific method resolution order or stating a rule for combining methods.
- Eiffel handles this situation by select and rename directives, where the ancestors' methods to use in a descendant are explicitly specified. This allows the methods of the base class to be shared between its descendants or to even 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
Band its ancestors would be checked before class
Cand its ancestors, so the method in
Awould be inherited through
B. In Perl, this behavior can be overridden using the
mroor other modules to use C3 linearization or other algorithms.
- 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 a classes using the C3 linearization algorithm. That algorithm enforces two constraints: children precede their parents and if a class inherits from multiple classes, they are kept in the order specified in the tuple of base classes. Thus, the method resolution order is:
- Scala resolves method names using a right-first depth-first search of extended 'traits', before eliminating all but the last occurrence of each module in the resulting list. So, the resolution order is: [
A], which reduces down to [
- JavaFX Script in version 1.2 allows multiple inheritance through the use of mixins. In case of conflict, the compiler prohibits the direct usage of the ambiguous variable or function. Each inherited member can still be accessed by casting the object to the mixin of interest, e.g.
(individual as Person).printInfo();.
Languages that allow only single inheritance (such as Ada, Ruby, Objective-C, PHP, C#, Delphi/Free Pascal and Java) allow the multiple inheritance of interfaces (called protocols in Objective-C). Interfaces are essentially abstract base classes with all abstract methods and no data members. The problem is therefore less likely since there is in most cases only one implementation of a specific method or property. Ambiguity can arise when multiple interfaces declare the same methods and the class defines them separately. See explicit interfaces.
The diamond problem is not limited to inheritance. It also arises in languages such as C and C++ when header files A, B, C, and D
#includeone 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 and the
#ifndefinclude guard is ineffective. It also is found when composing middleware stacks; for example, if A is a database and B and C are caches, D may ask both B and C to commit a transaction, resulting in duplicate commit calls to A.
- Eddy Truyen, Wouter Joosen, Bo Nørregaard Jørgensen, Pierre Verbaeten (2004). "A Generalization and Solution to the Common Ancestor Dilemma Problem in Delegation-Based Object Systems". Proceedings of the 2004 Dynamic Aspects Workshop (103–119). http://www.cs.kuleuven.ac.be/~eddy/PUBLICATIONS/DAW2004.pdf.
- C++ by default follows each inheritance path separately, so a
Wikimedia Foundation. 2010.
Look at other dictionaries:
Diamond-Problem — Vererbungsbeziehungen beim Diamond Problem Das Diamond Problem entsteht durch Mehrfachvererbung in der Objektorientierten Programmierung und Wissensmodellierung. Es kann auftreten, wenn eine Klasse D auf zwei verschiedenen Vererbungspfaden (B und … Deutsch Wikipedia
Diamond — steht für: Diamant oder Rhombus (Rautenform) in der englischen Sprache Diamond Aircraft, ein österreichischer Flugzeughersteller Diamond (Cyclecar), eine Marke des US amerikanischen Automobilherstellers Cycle Car Company Diamond Light Source,… … Deutsch Wikipedia
Diamond (disambiguation) — Diamond is the hardest known natural material. Diamond, ♦ or ◊ may also refer to: Diamond (gemstone) Diamonds, a suit of cards Alt codes, ♦, the character or symbol formed by using the Alt key in conjunction with the Number Pad (Alt+num4) Lozenge … Wikipedia
Diamond Multimedia — Headquarters Chatsworth, California Products Graphics cards, sound cards, Digital audio players Website … Wikipedia
Diamond wire cutting — (DWC) is the process of using wire of various diameters and lengths, impregnated with diamond dust of various sizes to cut through materials. Because of the hardness of diamonds, this cutting technique can cut through almost any material that is… … Wikipedia
Diamond v. Diehr — Supreme Court of the United States Argued October 14, 1980 Decided March 3, 1981 … Wikipedia
Diamond Hoo Ha — Studio album by Supergrass Released 24 March 2008 … Wikipedia
Diamond Shoal Light — Undated photograph of Diamond Shoal Light (USCG) Coordinates … Wikipedia
Diamond Cut Productions — is an American company founded by Craig Maier and Rick Carlson in 1986 87 with the aim of preserving many of the original test pressing recordings made at the Edison Laboratories during the 1920s. Diamond Cut Productions is the author of the… … Wikipedia
Diamond Geezer 2 — Format Comedy Drama Created by Caleb Ranson Starring David Jason Gary Whelan Paul Bown Stephen Wight George Cole Jenny Agutter Country of origi … Wikipedia