Multiple inheritance

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 sensitive 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 said 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.

Details

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.

Implementations

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, Perl 6, POP-11, Python, and Tcl (built-in from 8.6 or via Incremental Tcl (Incr Tcl) in earlier versions).

IBM System Object Model 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 C#, Java, 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

A diamond class inheritance diagram.

The "diamond problem" (sometimes referred to as the "deadly diamond of death"[4]) 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.

Mitigation

Languages have different ways of dealing with these problems of repeated inheritance.

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 (Delphi), Objective-C, Smalltalk, Swift and PHP.[12] All but Smalltalk allow classes to implement multiple protocols.

Moreover, languages such as Ada, Objective-C, C#, Delphi/Free Pascal, Java, 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 behavior. ("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.

See also

References

  1. [Cargill, T. A. (1991) Controversy: The case against multiple inheritance in C++, Computing Systems 4(1) 69-82]
  2. [Waldo, J. (1991) Controversy: The Case For Multiple Inheritance in C++. Computing Systems 4(2) 157-171]
  3. Nathanael Schärli; Stéphane Ducasse; Oscar Nierstrasz; Andrew Black. "Traits: Composable Units of Behavior" (pDF). Web.cecs.pdx.edu. Retrieved 2016-10-21.
  4. Martin, Robert C. (1997-03-09). "Java and C++: A critical comparison" (PDF). Objectmentor.com. Retrieved 2016-10-21.
  5. "Standard ECMA-367". Ecma-international.org. Retrieved 2016-10-21.
  6. "State of the Lambda". Cr.openjdk.java.net. Retrieved 2016-10-21.
  7. "Multiple Inheritance of State, Implementation, and Type (The Java Tutorials : Learning the Java Language : Interfaces and Inheritance)". Docs.oracle.com. Retrieved 2016-10-21.
  8. "perlobj". perldoc.perl.org. Retrieved 2016-10-21.
  9. Abstract:. "The Python 2.3 Method Resolution Order". Python.org. Retrieved 2016-10-21.
  10. "Unifying types and classes in Python 2.2". Python.org. Retrieved 2016-10-21.
  11. "Manpage of class". Tcl.tk. 1999-11-16. Retrieved 2016-10-21.
  12. "Object Interfaces - Manual". PHP.net. 2007-07-04. Retrieved 2016-10-21.

Further reading

External links

This article is issued from Wikipedia - version of the 10/24/2016. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.