Multiple inheritance

related topics
{math, number, function}
{system, computer, user}
{language, word, form}
{woman, child, man}
{style, bgcolor, rowspan}

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

Contents

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 the programmer to state which parent class the feature to be used is invoked 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).
  • Scala allows multiple instantiation of traits, which allows for multiple inheritance by adding a distinction between the class hierarchy and the trait hierarchy. A class can only inherit from a single class, but can mix-in as many traits as desired.
  • 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.

[edit] 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 (although soluble by using virtual inheritance).[3]
  • Not being able to explicitly inherit multiple times from a single class (on the other hand this feature is criticised as non-object-oriented).[citation needed]
  • Order of inheritance changing class semantics (although it's the same with order of field declarations).[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[citation needed]. 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]

Full article ▸

related documents
Partition of unity
Linear congruence theorem
Composite number
Group homomorphism
Lyapunov fractal
Best-first search
Permutation group
LALR parser
Nearest neighbour algorithm
Contraction mapping
Blum Blum Shub
Complete measure
Weak entity
EXPSPACE
Almost everywhere
Normal morphism
Pole (complex analysis)
Axiom of union
Chosen-plaintext attack
Continuity property
Sed
Tree structure
Sophie Germain prime
Data set
Zhu Shijie
Baire category theorem
Monoid ring
Arithmetic-geometric mean
NP-easy
Bilinear map