Jump to content

Multiple inheritance: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Chip Zero (talk | contribs)
rv POV statement
Clemahieu (talk | contribs)
Separated Multiple inheritance debate from description. Moved description to the top since this is an encyclopedia.
Line 1: Line 1:
'''Multiple inheritance''' refers to a feature of [[object-oriented programming|object-oriented]] [[programming language]]s in which a [[class (computer science)|class]] can inherit behaviors and features from more than one [[superclass (computer science)|superclass]]. This contrasts with '''single inheritance''', where a class inherits from only one superclass.
'''Multiple inheritance''' refers to a feature of [[object-oriented programming|object-oriented]] [[programming language]]s in which a [[class (computer science)|class]] can [[inheritance (computer science)|inherit]] behaviors and features from more than one [[superclass (computer science)|superclass]]. This contrasts with '''single inheritance''', where a class may inherit from only one superclass.


Languages that mostly support multiple inheritance are: Eiffel, C++, Perl, and CLOS.
Multiple inheritance can cause some confusing situations, so there is some debate over whether or not its benefits outweigh its risks. [[Java (programming language)|Java]] compromises: it allows a class to inherit [[interface (Java)|interface]]s from more than one parent (that is, one can specify that a class inherits all the types from its parents and must have all of the same externally exposed methods of its interface-parents, and allow the compiler to enforce that), but can inherit implementation (methods and fields) from only one parent. Microsoft's [[.NET Framework|.NET]] languages such as [[C Sharp|C#]] and [[Visual Basic .NET|Visual Basic]] implement this interface approach as well. In contrast [[C++]] implements full multiple inheritance.


Multiple inheritance allows a class to take on functionaly 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.
In object-oriented programming (OOP), '''[[inheritance (object-oriented programming)|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".


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. You would then have the following rules:
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).


StudentMusician: Person, Musician, Worker<br/>
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]]''.
Musician : Persor, Worker<br/>
Worker: Person


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. [[Eiffel programming language|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 inherit multiple interfaces but only a single implementation. [[REALbasic programming language|REALbasic]] is similar, but also allows additional methods to "extend" a class without inheritance. [[Perl]] uses the list of classes to inherit from as an ordered list, and uses the first method it finds by depth-first searching first the current class, then the inherited classes in the order listed and their superclasses. [[CLOS]] allows for complete programmer control of method combination, and if that's not enough the [[meta-object 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. [[Logtalk]] supports both interface and implementation multi-inheritance, allowing the declaration of method ''aliases'' that provide both renaming and acess to methods that would be masked out by the default conflict resolution mechanism.

Languages have different ways of dealing with these problems of repeated inheritance.
*[[Eiffel programming language|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.
*C++ requires that the programmer state which parent class the feature to use should come from i.e. "Worker::Person.Age" C++ does not support explicit repeated inheritance since there would be no way to qualify which superclass to use.
*[[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.
*[[CLOS]] allows for complete programmer control of method combination, and if that's not enough the [[meta-object 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.
*[[Logtalk]] supports both interface and implementation multi-inheritance, allowing the declaration of method ''aliases'' that provide both renaming and acess to methods that would be masked out by the default conflict resolution mechanism.
*Java and C# only allow classes to inherit from one superclass, this results in no abiguity. Java and C# allow classes to from multiple [[Interface (Computer Science)|interfaces]].

==Debate==
There is debate as to whether multiple inheritance can be implemented simply and without ambiguity. Detractors point out multiple inheritance implementation problems such as not being able to explicitly inherit from multiple classes and the order of inheritance changing class semantics. There are languages that address all technical issues of multiple inheritance so the main debate is whether implementing and using multiple inheritance is easier than using single inheritance and software design patterns.


==See also==
==See also==
Line 18: Line 31:


== External links ==
== External links ==
* [http://builder.com.com/5100-6373-5030734.html Article by Jonathan Lurie of Builder.Com on instances in .NET languages]
* [http://builder.com.com/5100-6373-5030734.html Article by Jonathan Lurie of Builder.Com on instances in .NET languages]
* [http://archive.eiffel.com/doc/online/eiffel50/intro/language/tutorial-10.html#pgfId-515052 Tutorial on inheritance usage in Eiffel]


[[Category:Object-oriented programming]]
[[Category:Object-oriented programming]]

Revision as of 06:14, 13 February 2007

Multiple inheritance refers to a feature of 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 only one superclass.

Languages that mostly support multiple inheritance are: Eiffel, C++, Perl, and CLOS.

Multiple inheritance allows a class to take on functionaly 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. You would then have the following rules:

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

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 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++ requires that the programmer state which parent class the feature to use should come from i.e. "Worker::Person.Age" C++ does not support explicit repeated inheritance since there would be no way to qualify which superclass to use.
  • 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.
  • CLOS allows for complete programmer control of method combination, and if that's not enough the meta-object 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.
  • Logtalk supports both interface and implementation multi-inheritance, allowing the declaration of method aliases that provide both renaming and acess to methods that would be masked out by the default conflict resolution mechanism.
  • Java and C# only allow classes to inherit from one superclass, this results in no abiguity. Java and C# allow classes to from multiple interfaces.

Debate

There is debate as to whether multiple inheritance can be implemented simply and without ambiguity. Detractors point out multiple inheritance implementation problems such as not being able to explicitly inherit from multiple classes and the order of inheritance changing class semantics. There are languages that address all technical issues of multiple inheritance so the main debate is whether implementing and using multiple inheritance is easier than using single inheritance and software design patterns.

See also