In OOP, is it possible to have a class that inherits from multiple other classes? And if yes, how is this situation called?

For example, suppose I have the following classes:

Eye
Nose
Mouth
Eyebrow
Ear
Hair
Skin

Now I want to have a class called Face which should inherit or depend on all these classes to make a Face object. In other words I want to assemble many classes into one class.

  • 2
    I don't think you want inheritance in your example but rather composition. Face should have members Eye, Nose, etc. but not actually implement them. – Dan Wilson Oct 10 at 19:57
  • 4
    The term is "how to use inheritance wrong" ;-) – Doc Brown Oct 10 at 19:58
  • 5
    Possible duplicate of Why should I prefer composition over inheritance? – Doc Brown Oct 10 at 19:59
  • The "is it possible" question is easily answered with "it depends if the language allows it". It being a good idea is a completely different pair of shoes. For your example in fact, it would be a terrible idea, as you probably want Face to be a container for all the other things, not a subclass. – BgrWorker Oct 11 at 15:57
up vote 6 down vote accepted

As you start to learn on OOP, one of the first thing that you need to learn is to prefer composition over inheritance.

In your example, a face has eyes, mouth, ears, .... When an object of a class assembles objects from other classes in that way, it is called composition. This is what you need.

This being said, and to satisfy your curiosity about inheritance:

  • inheritance is a very special relationship that should mean is-a: a Dog is-an Animal, so it may inherit from it.
  • most OOP languages allow multilevel inheritance, where one class inherits from another class with inherits from a third: a GermanShepherd inherits from Dog which inherits from Animal.
  • some OOP languages allow multiple inheritance where one class can inherit from several others (example: a Dog could inherit from Pet and have a method showAffection(), and at the same time inherit from Animal and have a method findFood().
  • many OOP languages don't provide multiple inheritance, but propose the concept of interface to achieve similar effects. (The main difference is that the class has to implement itself the behaviors promised by its multiple interfaces.)
  • Very nice job after your edit - I wouldn't have bothered with my answer. – user949300 Oct 10 at 21:14
  • You might mention mixin inheritance. Mixin inheritance subsumes almost all classical forms of inheritance: Simula-style class-based inheritance (as in Smalltalk, Objective-C, C++, PHP, Python, Ruby, …), Beta-style prefixing (where superclass methods call the subclass implementation instead of subclasses calling super), and multiple inheritance with linearization (e.g. CLOS, Dylan, Python). – Jörg W Mittag Oct 10 at 23:29

You want Composition, not Inheritance, Here's a good overview of Inheritance vs Composition.

To elaborate, Inheritance represents a possible "is a" relationship. For example, if you have a Letter class, the particular letter M "is-a" letter. If you have a Vehicle class, "Audi A4" is a Car, which could be a subclass of vehicle. (though you might not want inheritance here either, YMMV)

Composition represents a "has a" relationship. My garage is not a Car, but it "has a" car. "Audi" is not a letter, but is has a (bunch of) letters.

In your example, nobody would say that the face "is an" eye. But it "has" 0, 1, or 2 eyes.

Modern "best practice" is to favor composition over inheritance, so, when it doubt, don't subclass.

[This is in addition to @Christophe's answer.]

In OOP, is it possible to have a class inherits from another multiple classes? and if it is true, what is the term of this situation?

The term for what's proposed in the original question is multiple inheritance. For the purposes of this conversation, inheritance can be divided into two flavors: implementation inheritance and interface inheritance. Hardly any OO languages support multiple implementation inheritance, because rarely can it be implemented well. Many OO languages support single implementation inheritance and multiple interface inheritance.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.