Multiple inheritance
Multiple inheritance refers to a feature of some object-oriented programming languages in which a class can inherit behaviors and features from more than one superclass. This contrasts with single inheritance, where a class may inherit from at most one superclass.
Languages that support multiple inheritance include: C++, Common Lisp (via CLOS), EuLisp (via The EuLisp Object System TELOS), Curl, Dylan, Eiffel, Logtalk, Object REXX, Scala (via the use of mixin classes), OCaml, Perl, Perl 6, Python, and Tcl (via Incremental Tcl)[1].
Overview
Multiple inheritance allows a class to take on functionality from multiple other classes, such as allowing a class named StreetMusician
to inherit from a class named Human
, a class named Musician
, and a class named Worker
. This can be abbreviated StreetMusician : Human, Musician, Worker
.
Ambiguities arise in multiple inheritance, as in the example above, if for instance the class Musician inherited from Human
and Worker
and the class Worker
inherited from Human
. This is referred to as the Diamond problem. There would then be the following rules:
Worker : Human Musician : Human, Worker StreetMusician : Human, Musician, Worker
If a compiler is looking at the class StreetMusician
, it needs to know whether it should join identical features together, or whether they should be separate features. For instance, it would make sense to join the "Age" features of Human together for StreetMusician
. A human's age doesn't change, if you consider them a human, a worker, or a musician. It would, however, make sense to separate the feature "Name" in Human
and Musician
, if they use a different stage name than their given name. The options of joining and separating are both valid in their own context and only the programmer knows which option is correct for the class they are designing.
Languages have different ways of dealing with these problems of repeated inheritance.
- C++ requires that the programmer states which parent class the feature to use should come 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 (see criticisms). 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).
- The Common Lisp Object System allows full programmer control of method combination, and if this is not enough, the Metaobject Protocol gives the programmer a means to modify the inheritance, method dispatch, class instantiation, and other internal mechanisms without affecting the stability of the system.
- Curl allows only classes that are explicitly marked as shared to be inherited repeatedly. Shared classes must define a secondary constructor for each regular constructor in the class. The regular constructor is called the first time the state for the shared class is initialized through a subclass constructor, and the secondary constructor will be invoked for all other subclasses.
- Eiffel allows the programmer to explicitly join or separate features that are being inherited from superclasses. Eiffel will automatically join features together, if they have the same name and implementation. The class writer has the option to rename the inherited features to separate them. Eiffel also allows explicit repeated inheritance such as A: B, B.
- Logtalk supports both interface and implementation multi-inheritance, allowing the declaration of method aliases that provide both renaming and access to methods that would be masked out by the default conflict resolution mechanism.
- Ocaml chooses the last matching definition of a class inheritance list to resolve which method implementation to use under ambiguities. To override the default behavior, one simply qualifies a method call with the desired class definition.
- Perl uses the list of classes to inherit from as an ordered list. The compiler uses the first method it finds by depth-first searching of the superclass list or using the C3 linearization of the class hierarchy. Various extensions provide alternative class composition schemes. The order of inheritance affects the class semantics (see criticisms).
- Python has the same structure as Perl, but unlike Perl includes it in the syntax of the language. The order of inheritance affects the class semantics (see criticisms).
- Tcl allows multiple parent classes- their serial affects the name resolution for class members.[2]
C#, Object Pascal / Delphi, Java, Nemerle, Objective-C, Smalltalk, and PHP do not allow multiple inheritance, and this avoids any ambiguity. However, all but Smalltalk allow classes to implement multiple interfaces.
Criticisms
Multiple inheritance has been criticised for the following problems that it causes in certain languages, in particular C++:
- Semantic ambiguity often summarized as the diamond problem.[3]
- Not being able to explicitly inherit multiple times from a single class.[citation needed]
- Order of inheritance changing class semantics.[citation needed]
Multiple inheritance in languages with C++-/Java-style constructors exacerbates the inheritance problem of constructors and constructor chaining, thereby creating maintenance and extensibility problems in these languages. Objects in inheritance relationship with greatly varying construction methods are hard to implement under the constructor-chaining paradigm.
There are languages that address these technical issues, however.[not specific enough to verify]
See also
References
Further reading
- Stroustrup, Bjarne (1999). Multiple Inheritance for C++. Proceedings of the Spring 1987 European Unix Users Group Conference.
- Object-Oriented Software Construction, Second Edition, by Bertrand Meyer, Prentice Hall, 1997, ISBN 0-13-629155-4