Multiple Inheritance

From CSSEMediaWiki
(Difference between revisions)
Jump to: navigation, search
m (Reverted edits by Ebybymic (Talk); changed back to last version by Matthew Harward)
 
(15 intermediate revisions by 3 users not shown)
Line 1: Line 1:
Multiple Inheritance (MI) occurs when a subclass has two or more super classes.  
+
Multiple Inheritance (MI) occurs when a subclass has two or more super classes.
  
It is an available construct in C++, Eiffel and Perl. However, it is not available in Java or C#.  
+
It is an available construct in some object oriented languages including C++, Common Lisp, Curl, Dylan, Eiffel, OCaml, Perl and Python. However, it is not available in Java or C#.
 +
 
 +
Multiple Inheritance is often considered a desirable feature of OO languages however, Riel claims, with relation to the introduction of MI to C++, that ''"...only 2 percent [of developers] understood why they wanted multiple inheritance and were not misusing it."''
 +
 
 +
===Acceptable Usage===
 +
Riel believes it is acceptable to use MI when the two classes that are being subclasses provide two real concepts that both provide a necessary, objective, component of the created subclass.
 +
 
 +
[[Image:MIPenguin.png]]
 +
 
 +
===Unacceptable Usage===
 +
Riel believes that it is not acceptable to use MI when:
 +
* it is for the purpose of composition, e.g. an Aircraft should not inherit from Wings or a Cockpit.
 +
* the inheritance hierarchy is poorly defined, e.g an Orange should not inherit from Citrus and Food. It should inherit from Citrus, which should, in turn, inherit from Food.
 +
 
 +
==='Mixins' and DAGs===
 +
''Coming soon!''
 +
 
 +
===Non-MI Languages===
 +
In languages that do not support MI, several techniques can be used to provide equivalent functionality.
 +
* The essential superclass can still be defined.
 +
* Subclasses can aggregate instances of classes designed to provide the functionality of the other superclasses. In English, in order to provide the functionality lost by not using multiple inheritance, instances of classes can hold objects designed to provide the same facilities as the missing superclasses.
 +
* Interfaces can be used to make sure classes support specific methods and have a specific form (most OO languages can implement multiple interfaces).
  
 
==See Also==
 
==See Also==
 +
* [[Inheritance]]
 +
* [[Riel's heuristics]]
 
* [[Avoid multiple inheritance]]
 
* [[Avoid multiple inheritance]]
 +
* [[Beware inheritance over composition]]
 +
* [[Avoid accidental multiple inheritance]]
 +
* [[Diamond Problem]]
 +
 +
{{Nomenclature}}
 +
 +
[[Category: Nomenclature]]

Latest revision as of 03:22, 25 November 2010

Multiple Inheritance (MI) occurs when a subclass has two or more super classes.

It is an available construct in some object oriented languages including C++, Common Lisp, Curl, Dylan, Eiffel, OCaml, Perl and Python. However, it is not available in Java or C#.

Multiple Inheritance is often considered a desirable feature of OO languages however, Riel claims, with relation to the introduction of MI to C++, that "...only 2 percent [of developers] understood why they wanted multiple inheritance and were not misusing it."

Contents

Acceptable Usage

Riel believes it is acceptable to use MI when the two classes that are being subclasses provide two real concepts that both provide a necessary, objective, component of the created subclass.

MIPenguin.png

Unacceptable Usage

Riel believes that it is not acceptable to use MI when:

  • it is for the purpose of composition, e.g. an Aircraft should not inherit from Wings or a Cockpit.
  • the inheritance hierarchy is poorly defined, e.g an Orange should not inherit from Citrus and Food. It should inherit from Citrus, which should, in turn, inherit from Food.

'Mixins' and DAGs

Coming soon!

Non-MI Languages

In languages that do not support MI, several techniques can be used to provide equivalent functionality.

  • The essential superclass can still be defined.
  • Subclasses can aggregate instances of classes designed to provide the functionality of the other superclasses. In English, in order to provide the functionality lost by not using multiple inheritance, instances of classes can hold objects designed to provide the same facilities as the missing superclasses.
  • Interfaces can be used to make sure classes support specific methods and have a specific form (most OO languages can implement multiple interfaces).

See Also


Personal tools