Multiple Inheritance

From CSSEMediaWiki
Revision as of 03:22, 25 November 2010 by WikiSysop (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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."


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.


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