Multiple inheritance is a feature of some object-oriented computer programming languages in which an object or class can inherit characteristics and features from more than one parent object or parent class. It is distinct from single inheritance, where an object or class may only inherit from one particular object or class.
Multiple inheritance has been a controversial issue for many years,[1][2] with opponents pointing to its increased complexity and ambiguity in situations such as the "diamond problem", where it may be ambiguous as to which parent class a particular feature is inherited from if more than one parent class implements same feature. This can be addressed in various ways, including using virtual inheritance.[3] Alternate methods of object composition not based on inheritance such as mixins and traits have also been proposed to address the ambiguity.
In object-oriented programming (OOP), inheritance describes a relationship between two classes in which one class (the child class) subclasses the parent class. The child inherits methods and attributes of the parent, allowing for shared functionality. For example, one might create a variable class Mammal with features such as eating, reproducing, etc.; then define a child class Cat that inherits those features without having to explicitly program them, while adding new features like chasing mice.
Multiple inheritance allows programmers to use more than one totally orthogonal hierarchy simultaneously, such as allowing Cat to inherit from Cartoon character and Pet and Mammal and access features from within all of those classes.
Languages that support multiple inheritance include: C++, Common Lisp (via Common Lisp Object System (CLOS)), EuLisp (via The EuLisp Object System TELOS), Curl, Dylan, Eiffel, Logtalk, Object REXX, Scala (via use of mixin classes), OCaml, Perl, POP-11, Python, R, Raku, and Tcl (built-in from 8.6 or via Incremental Tcl (Incr Tcl) in earlier versions[4][5]).
IBM System Object Model (SOM) runtime supports multiple inheritance, and any programming language targeting SOM can implement new SOM classes inherited from multiple bases.
Some object-oriented languages, such as Swift, Java, C#, and Ruby implement single inheritance, although protocols, or interfaces, provide some of the functionality of true multiple inheritance.
PHP uses traits classes to inherit specific method implementations. Ruby uses modules to inherit multiple methods.
The "diamond problem" (sometimes referred to as the "Deadly Diamond of Death"[6]) is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C. If there is a method in A that B and C have overridden, and D does not override it, then which version of the method does D inherit: that of B, or that of C?
For example, in the context of GUI software development, a class Button
may inherit from both classes Rectangle
(for appearance) and Clickable
(for functionality/input handling), and classes Rectangle
and Clickable
both inherit from the Object
class. Now if the equals
method is called for a Button
object and there is no such method in the Button
class but there is an overridden equals
method in Rectangle
or Clickable
(or both), 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 case, 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.
Languages have different ways of dealing with these problems of repeated inheritance.
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
" (for example, "class B : virtual public 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
. C++ requires stating explicitly which parent class the feature to be used is invoked from i.e. Worker::Human.Age
. C++ does not support explicit repeated inheritance since there would be no way to qualify which superclass to use (i.e. having a class appear more than once in a single derivation list [class Dog : public Animal, Animal]). C++ also allows a single instance of the multiple class to be created via the virtual inheritance mechanism (i.e. Worker::Human
and Musician::Human
will reference the same object).D,B,C,A
, when B is written before C in the class definition. The method with the most specific argument classes is chosen (D>(B,C)>A) ; then in the order in which parent classes are named in the subclass definition (B>C). However, the programmer can override this, by giving a specific method resolution order or stating a rule for combining methods. This is called method combination, which may be fully controlled. The MOP (metaobject protocol) also provides means to modify the inheritance, dynamic dispatch, class instantiation, and other internal mechanisms without affecting the stability of the system.D
embeds two structures B
and C
which both have a method F()
, thus satisfying an interface A
, the compiler will complain about an "ambiguous selector" if D.F()
is called, or if an instance of D
is assigned to a variable of type A
. B
and C
's methods can be called explicitly with D.B.F()
or D.C.F()
.A,B,C
are interfaces, B,C
can each provide a different implementation to an abstract method of A
, causing the diamond problem. Either class D
must reimplement the method (the body of which can simply forward the call to one of the super implementations), or the ambiguity will be rejected as a compile error.[8] Prior to Java 8, Java was not subject to the Diamond problem risk, because it did not support multiple inheritance and interface default methods were not available.(individual as Person).printInfo();
. super<ChosenParentInterface>.someMethod()
B
and its ancestors would be checked before class C
and its ancestors, so the method in A
would be inherited through B
. This is shared with Io and Picolisp. In Perl, this behavior can be overridden using the mro
or other modules to use C3 linearization or other algorithms.[9]object
. Python creates a list of classes using the C3 linearization (or Method Resolution Order (MRO)) 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 (however in this case, some classes high in the inheritance graph may precede classes lower in the graph[10]). Thus, the method resolution order is: D
, B
, C
, A
.[11]D
, C
, A
, B
, A
], which reduces down to [D
, C
, B
, A
].Languages that allow only single inheritance, where a class can only derive from one base class, do not have the diamond problem. The reason for this is that such languages have at most one implementation of any method at any level in the inheritance chain regardless of the repetition or placement of methods. Typically these languages allow classes to implement multiple protocols, called interfaces in Java. These protocols define methods but do not provide concrete implementations. This strategy has been used by ActionScript, C#, D, Java, Nemerle, Object Pascal, Objective-C, Smalltalk, Swift and PHP.[13] All these languages allow classes to implement multiple protocols.
Moreover, Ada, C#, Java, Object Pascal, Objective-C, Swift and PHP allow multiple-inheritance of interfaces (called protocols in Objective-C and Swift). Interfaces are like abstract base classes that specify method signatures without implementing any behaviour. ("Pure" interfaces such as the ones in Java up to version 7 do not permit any implementation or instance data in the interface.) Nevertheless, even when several interfaces declare the same method signature, as soon as that method is implemented (defined) anywhere in the inheritance chain, it overrides any implementation of that method in the chain above it (in its superclasses). Hence, at any given level in the inheritance chain, there can be at most one implementation of any method. Thus, single-inheritance method implementation does not exhibit the Diamond Problem even with multiple-inheritance of interfaces. With the introduction of default implementation for interfaces in Java 8 and C# 8, it is still possible to generate a Diamond Problem, although this will only appear as a compile-time error.
By: Wikipedia.org
Edited: 2021-06-18 19:25:48
Source: Wikipedia.org