Multiple inheritance

Multiple inheritance

Multiple inheritance is a feature of some object-oriented computer programming languages in which a class can inherit behaviors and features from more than 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]

Other object-oriented languages, such as Java and Ruby implement single inheritance, although protocols, or "interfaces," provide some of the functionality of true multiple inheritance.

Contents

Overview

In object-oriented programming (OOP), inheritance describes a relationship between two types, or classes, of objects in which one is said to be a "subtype" or "child" of the other. The child inherits features of the parent, allowing for shared functionality. For example, one might create a variable class "Mammal" with features such as eating, reproducing, etc.; then define a subtype "Cat" that inherits those features without having to explicitly program them, while adding new features like "chasing mice".

If, however, one wants to use more than one totally orthogonal hierarchy simultaneously, such as allowing "Cat" to inherit from "Cartoon character" and "Pet" as well as "Mammal", lack of multiple inheritance often results in a very awkwardly mixed hierarchy, or forces functionality to be rewritten in more than one place (with the attendant maintenance problems).

Multiple inheritance has been a touchy issue for many years, with opponents pointing to its increased complexity and ambiguity in situations such as the diamond problem.

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]

Single inheritance

One way of resolving these problems has been to enforce single inheritance where a class can only derive from one base class. Typically these languages allow classes to implement multiple protocols, called interfaces in Java. These protocols define methods but do not provide concrete implementations. This strategy has been used by C#, D, Object Pascal / Delphi, Java, ActionScript, Nemerle, Objective-C, Ruby, Smalltalk and PHP. All but Smalltalk allow classes to implement multiple protocols.

Criticisms

Multiple inheritance has been criticized by some for its unnecessary complexity and being difficult to implement efficiently, though some projects have certainly benefited from its use.[citation needed] Java, for example has no multiple inheritance, as its designers felt that it would add unnecessary complexity.[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.

Criticisms for the problems that it causes in certain languages, in particular C++, are:

  • Semantic ambiguity often summarized as the diamond problem (although solvable by using virtual inheritance or 'using' declarations).[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]

See also

References

  1. ^ Tcl Advocacy
  2. ^ Tcl Manual:class
  3. ^ Traits: Composable Units of Behavior

Further reading

External links


Wikimedia Foundation. 2010.

Игры ⚽ Поможем решить контрольную работу

Look at other dictionaries:

  • multiple inheritance — noun The ability to inherit from more than one superclass in a single subclass …   Wiktionary

  • Inheritance (object-oriented programming) — In object oriented programming (OOP), inheritance is a way to reuse code of existing objects, establish a subtype from an existing object, or both, depending upon programming language support. In classical inheritance where objects are defined by …   Wikipedia

  • Inheritance (computer science) — In object oriented programming, inheritance is a way to form new classes (instances of which are called objects) using classes that have already been defined. The inheritance concept was invented in 1967 for Simula. [ [http://heim.ifi.uio.no/… …   Wikipedia

  • Multiple endocrine neoplasia — Classification and external resources Micrograph of a medullary thyroid carcinoma, as may be seen in MEN 2A and MEN 2B. H E stain …   Wikipedia

  • Multiple epiphyseal dysplasia — Classification and external resources ICD 9 756.56 OMIM 132400 226900 …   Wikipedia

  • inheritance — /in her i teuhns/, n. 1. something that is or may be inherited; property passing at the owner s death to the heir or those entitled to succeed; legacy. 2. the genetic characters transmitted from parent to offspring, taken collectively. 3.… …   Universalium

  • Inheritance — Not something that is contained in a will, but rather a gene, chromosome or genome that is transmitted from parent to child. The pattern of inheritance is the manner in which a gene is transmitted. For example, the pattern of inheritance may be… …   Medical dictionary

  • Multiple symmetric lipomatosis — A disorder characterized by painless symmetrical diffuse deposits of fat beneath the skin of the neck, upper trunk, arms and legs. The condition is thought to be genetic although its exact mode of inheritance is uncertain. It may be a… …   Medical dictionary

  • multiple factor — noun 1. : multiple allele 2. : one of a group of nonallelic genes that according to the multiple factor hypothesis control various quantitative hereditary characters (as size and skin color) compare polygene, quantitative inheritance …   Useful english dictionary

  • multiple eruptive milia — a type of colloid milia seen in children, transmitted by autosomal dominant inheritance …   Medical dictionary

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”