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."
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.
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
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).
- Riel's heuristics
- Avoid multiple inheritance
- Beware inheritance over composition
- Avoid accidental multiple inheritance
- Diamond Problem
|Techniques: Abstraction | Aggregation versus Composition | Association versus Dependency | Coupling | Encapsulation | Information hiding | Inheritance | Multiple Inheritance | Overloading | Polymorphism|