Encyclopedia > Multiple inheritance

  Article Content

Multiple inheritance

Some programming languages allow multiple inheritance, in which a class can inherit behaviors and features from more than one superclass. This contrasts with single inheritance, where a class inherits from only one superclass.

Multiple inheritance can cause some confusing situations, so there is some debate over whether or not its benefits outweigh its risks. Java compromises: it allows a class to inherit interfaces from more than one parent (that is, one can specify that a class must have all of the same externally exposed methods of its interface-parents, and allow the compiler to enforce that), but can inherit actual methods and data from only one parent.

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 "Rat" that inherits those features without having to explicitly program them, while adding new features like collecting garbage.

If, however, you want to follow more than one totally orthogonal hierarchy simultaneously, such as allowing "Rat" to inherit from "Cartoon character" and "Disease vector" as well as "Mammal", without multiple inheritance you often end up with a very awkwardly mixed hierarchy, or rewriting functionality in more than one place (with the attendant maintenance problems).

Multiple inheritance has been a touchy issue for many, with opponents pointing to its increased complexity and ambiguity (for example, if two parents have independent implementations of a feature named X, which one do I inherit?)

Languages have different ways of dealing with these problems. Eiffel, for example, allows subtypes to adapt their inherited features by renaming them or setting selection rules for them ahead of time. Java allows objects to multiply inherit interfaces but only singly inherit implementations. Perl uses the list of classes to inherit from as an ordered list, and uses the first method it finds by searching first the current class, then the inherited classes in the order listed.



All Wikipedia text is available under the terms of the GNU Free Documentation License

 
  Search Encyclopedia

Search over one million articles, find something about almost anything!
 
 
  
  Featured Article
Johann Karl Friedrich Rosenkranz

... of philosophy at Halle for two years, he became, in 1833, professor at the university of Königsberg[?], where he remained till his death. In his last years he was quite ...

 
 
 
This page was created in 24.5 ms