Skip to main content

OOP and class that inherit from many classes [Resolved]

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.


Question Credit: SaidbakR
Question Reference
Asked October 10, 2018
Posted Under: Programming
37 views
3 Answers

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.


credit: user949300
Answered October 10, 2018
Your Answer
D:\Adnan\Candoerz\CandoProject\vQA