All Topics  
Multiple inheritance

 

   Email Print
   Bookmark   Link






 

Multiple inheritance



 
 
Multiple inheritance refers to a feature of some object-oriented
Object-oriented programming

Object-oriented programming is a programming paradigm that uses "Object_" and their interactions to design applications and computer programs....
 programming language
Programming language

A programming language is a machine-readable artificial language designed to express computations that can be performed by a machine, particularly a computer....
s in which a class
Class (computer science)

In object-oriented programming, a class is a programming language construct that is used as a blueprint to create Object s. This blueprint includes Attribute s and Method s that the created objects all share....
 can inherit
Inheritance (computer science)

In object-oriented programming, inheritance is a way to form new class es using classes that have already been defined. The inheritance concept was invented in 1967 for Simula....
 behaviors and features from more than one superclass
Superclass (computer science)

In computer science, a superclass is a class from which other classes are derived. A superclass is also called a parent class. The classes that are derived from a superclass are known as child classes, derived classes, or subclass ....
. This contrasts with single inheritance, where a class may inherit from at most one superclass.

Languages that support multiple inheritance include: Eiffel
Eiffel (programming language)

Eiffel is an International Organization for Standardization-standardized, object-oriented programming language designed to enable programmers to efficiently develop extensible, reusable, reliable software....
, C++
C++

C++ is a general-purpose programming language. It is regarded as a middle-level language, as it comprises a combination of both high-level programming language and low-level programming language language features....
, Dylan, Python
Python (programming language)

Python is a general-purpose high-level programming language. Its design philosophy emphasizes code readability. Python's core syntax and semantics are Minimalism , while the standard library is large and comprehensive....
, Perl
Perl

In computer programming, Perl is a high-level programming language, List of programming languages by category, Interpreter , dynamic programming language....
, Curl, Common Lisp
Common Lisp

Common Lisp, commonly abbreviated CL, is a dialect of the Lisp programming language, published in American National Standards Institute standard document Information Technology - Programming Language - Common Lisp, formerly X3.226-1994 ....
 (via CLOS), OCaml, and Tcl
Tcl

Tcl is a scripting language created by John Ousterhout. Originally "born out of frustration"?according to the author?with programmers devising their own languages intended to be embedded into applications, Tcl quickly gained wide acceptance on its own and is generally thought to be easy to learn, but powerful in competent hands....
 (via Incremental TCL
Itcl

incr Tcl is a set of object-oriented extensions for the Tcl programming language. It is widely used among the Tcl community, and is generally regarded as industrial strength ....
).

iple inheritance allows a class to take on functionality from multiple other classes, such as allowing a class named StudentMusician to inherit from a class named Person, a class named Musician, and a class named Worker.






Discussion
Ask a question about 'Multiple inheritance'
Start a new discussion about 'Multiple inheritance'
Answer questions from other users
Full Discussion Forum



Encyclopedia


Multiple inheritance refers to a feature of some object-oriented
Object-oriented programming

Object-oriented programming is a programming paradigm that uses "Object_" and their interactions to design applications and computer programs....
 programming language
Programming language

A programming language is a machine-readable artificial language designed to express computations that can be performed by a machine, particularly a computer....
s in which a class
Class (computer science)

In object-oriented programming, a class is a programming language construct that is used as a blueprint to create Object s. This blueprint includes Attribute s and Method s that the created objects all share....
 can inherit
Inheritance (computer science)

In object-oriented programming, inheritance is a way to form new class es using classes that have already been defined. The inheritance concept was invented in 1967 for Simula....
 behaviors and features from more than one superclass
Superclass (computer science)

In computer science, a superclass is a class from which other classes are derived. A superclass is also called a parent class. The classes that are derived from a superclass are known as child classes, derived classes, or subclass ....
. This contrasts with single inheritance, where a class may inherit from at most one superclass.

Languages that support multiple inheritance include: Eiffel
Eiffel (programming language)

Eiffel is an International Organization for Standardization-standardized, object-oriented programming language designed to enable programmers to efficiently develop extensible, reusable, reliable software....
, C++
C++

C++ is a general-purpose programming language. It is regarded as a middle-level language, as it comprises a combination of both high-level programming language and low-level programming language language features....
, Dylan, Python
Python (programming language)

Python is a general-purpose high-level programming language. Its design philosophy emphasizes code readability. Python's core syntax and semantics are Minimalism , while the standard library is large and comprehensive....
, Perl
Perl

In computer programming, Perl is a high-level programming language, List of programming languages by category, Interpreter , dynamic programming language....
, Curl, Common Lisp
Common Lisp

Common Lisp, commonly abbreviated CL, is a dialect of the Lisp programming language, published in American National Standards Institute standard document Information Technology - Programming Language - Common Lisp, formerly X3.226-1994 ....
 (via CLOS), OCaml, and Tcl
Tcl

Tcl is a scripting language created by John Ousterhout. Originally "born out of frustration"?according to the author?with programmers devising their own languages intended to be embedded into applications, Tcl quickly gained wide acceptance on its own and is generally thought to be easy to learn, but powerful in competent hands....
 (via Incremental TCL
Itcl

incr Tcl is a set of object-oriented extensions for the Tcl programming language. It is widely used among the Tcl community, and is generally regarded as industrial strength ....
).

Overview

Multiple inheritance allows a class to take on functionality from multiple other classes, such as allowing a class named StudentMusician to inherit from a class named Person, a class named Musician, and a class named Worker. This can be abbreviated StudentMusician : Person, Musician, Worker.

Ambiguities arise in multiple inheritance, as in the example above, if for instance the class Musician inherited from Person and Worker and the class Worker inherited from Person. This is referred to as the Diamond problem
Diamond problem

In object-oriented programming languages with multiple inheritance and knowledge organization, the diamond problem is an ambiguity that arises when two Class B and C Inheritance from A, and class D inherits from both B and C....
. There would then be the following rules:

Worker : Person Musician: Person, Worker StudentMusician : Person, Musician, Worker

If a compiler is looking at the class StudentMusician 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 Person together for StudentMusician. A person's age doesn't change if you consider them a Person, a Worker, or a Musician. It would, however, make sense to separate the feature "Name" in Person 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.
  • Eiffel
    Eiffel (programming language)

    Eiffel is an International Organization for Standardization-standardized, object-oriented programming language designed to enable programmers to efficiently develop extensible, reusable, reliable software....
     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.
  • C++
    C++

    C++ is a general-purpose programming language. It is regarded as a middle-level language, as it comprises a combination of both high-level programming language and low-level programming language language features....
     requires that the programmer state which parent class the feature to use should come from i.e. "WorkerPerson.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. "WorkerPerson" and "MusicianPerson" will reference the same object).
  • Perl
    Perl

    In computer programming, Perl is a high-level programming language, List of programming languages by category, Interpreter , dynamic programming language....
     uses the list of classes to inherit from as an ordered list. The compiler uses the first method it finds by depth-first search
    Depth-first search

    Depth-first search is an algorithm for traversing or searching a tree data structure, tree structure, or graph . One starts at the root and explores as far as possible along each branch before backtracking....
    ing of the superclass list or using the C3 linearization
    C3 linearization

    The C3 superclass linearization is an algorithm used primarily to obtain a consistent linearization of a multiple inheritance hierarchy in object-oriented programming....
     of the class hierarchy. Various extensions provide alternative class composition schemes. Python
    Python (programming language)

    Python is a general-purpose high-level programming language. Its design philosophy emphasizes code readability. Python's core syntax and semantics are Minimalism , while the standard library is large and comprehensive....
     has the same structure, but unlike Perl includes it in the syntax of the language. In Perl and Python, the order of inheritance affects the class semantics (see criticisms).
  • The Common Lisp Object System allows full programmer control of method combination, and if this is not enough, the Metaobject Protocol
    Metaobject

    In computer science, a metaobject or meta-object is any entity that manipulates, creates, describes, or implements other object s. The object that the metaobject is about is called the base object....
     gives the programmer a means to modify the inheritance, method dispatch, class instantiation, and other internal mechanisms without affecting the stability of the system.
  • Logtalk
    Logtalk

    Logtalk is an open source Object-oriented programming logic programming language that can use most Prolog implementations as a back-end compiler....
     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.
  • 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
    Constructor (computer science)

    In object-oriented programming, a constructor in a class is a special block of statements called when an object lifetime#Creating objects, either when it is declared or dynamically constructed on the heap-based memory allocation through the keyword ?new?....
     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.
  • 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.
  • Tcl
    Tcl

    Tcl is a scripting language created by John Ousterhout. Originally "born out of frustration"?according to the author?with programmers devising their own languages intended to be embedded into applications, Tcl quickly gained wide acceptance on its own and is generally thought to be easy to learn, but powerful in competent hands....
     allows multiple parent classes- their serial affects the name resolution for class members.


C#, Objective-C
Objective-C

Objective-C is a Reflection , Object-oriented programming programming language which adds Smalltalk-style message passing to C .Today it is used primarily on Mac OS X, iPhone OS, and GNUstep, three environments based on the OpenStep standard, and is the primary language used for the NEXTSTEP, OpenStep#OPENSTEP, and Cocoa application framew...
, Object Pascal / Delphi
Object Pascal

Object Pascal refers to a branch of Object-oriented programming derivatives of Pascal , mostly known as the primary programming language of CodeGear Delphi....
, Java
Java (programming language)

Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java ....
, Nemerle
Nemerle

Nemerle is a high level language static typing programming language for the Microsoft .NET platform. It offers functional programming, object-oriented and imperative programming features....
, and PHP
PHP

PHP is a scripting language originally designed for producing dynamic web pages. It has evolved to include a command line interface capability and can be used in Standalone software Graphical user interface....
 do not allow multiple inheritance, and this avoids any ambiguity. However, these six languages allow classes to inherit from multiple interfaces
Interface (computer science)

Interface generally refers to an Abstraction_%28computer_science%29 that an entity provides of itself to the outside. This separates the methods of external communication from internal operation, and allows it to be internally modified without affecting the way outside entities interact with it, as well as provide Polymorphism in object-orien...
, recreating some of the problems mentioned while avoiding others.

Criticisms

Multiple inheritance has received criticism and as such, is not implemented in many languages. Criticisms includes:
  • Increased complexity
    Programming Complexity

    Programming Complexity, which is often also referred to as Software Complexity is a term that encompasses numerous properties of a piece of software, all of which affect internal interactions....
  • Semantic ambiguity often summarized as the diamond problem
    Diamond problem

    In object-oriented programming languages with multiple inheritance and knowledge organization, the diamond problem is an ambiguity that arises when two Class B and C Inheritance from A, and class D inherits from both B and C....
    .
  • Not being able to explicitly inherit multiple times from a single class
  • Order of inheritance changing class semantics.


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 relationships with greatly varying construction methods are hard to implement under the constructor chaining paradigm.

See also

  • Implementation inheritance
    Implementation inheritance

    In programming, Implementation inheritance is the inheritance of the full functionality of a Class , as opposed to the inheritance of an interface , which simply defines the methods that must be present....
  • Virtual inheritance
    Virtual inheritance

    In the C++ programming language, virtual inheritance is a kind of Inheritance that solves some of the problems caused by multiple inheritance by clarifying ambiguity over which ancestor class members to use....
  • Mixin
    Mixin

    In object-oriented programming languages, a mixin is a class that provides a certain functionality to be Inheritance by a subclass, but is not meant to stand alone....
  • C3 linearization
    C3 linearization

    The C3 superclass linearization is an algorithm used primarily to obtain a consistent linearization of a multiple inheritance hierarchy in object-oriented programming....


Further reading

  • Stroustrup, Bjarne (1999). . Proceedings of the Spring 1987 European Unix Users Group Conference.
  • Object-Oriented Software Construction, Second Edition, by Bertrand Meyer
    Bertrand Meyer

    Bertrand Meyer is an academic, author, and consultant in the field of computer languages. He created the Eiffel ....
    , Prentice Hall, 1997, ISBN 0-13-629155-4


External links