-3

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.

Christophe
  • 74,672
  • 10
  • 115
  • 187
SaidbakR
  • 121
  • 5
  • 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 '18 at 19:57
  • 5
    The term is "how to use inheritance wrong" ;-) – Doc Brown Oct 10 '18 at 19:58
  • 6
    Possible duplicate of [Why should I prefer composition over inheritance?](https://softwareengineering.stackexchange.com/questions/134097/why-should-i-prefer-composition-over-inheritance) – Doc Brown Oct 10 '18 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 '18 at 15:57
  • 2
    Your face is not an eye. It is not a nose, or a mouth, or an eyebrow and so on. It has usually two eyes, sometimes fewer. It has one nose, sometimes fewer and so on. You absolutely 100% do NOT want to inherit from Eye, Nose and so on. – gnasher729 Aug 15 '22 at 12:21

3 Answers3

6

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.)
Christophe
  • 74,672
  • 10
  • 115
  • 187
  • Very nice job after your edit - I wouldn't have bothered with my answer. – user949300 Oct 10 '18 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 '18 at 23:29
3

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.

user949300
  • 8,679
  • 2
  • 26
  • 35
0

[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.

Nick Alexeev
  • 2,484
  • 2
  • 17
  • 24