QamoosTech
Programming FundamentalsObject-oriented programmingIntermediatePython

Multiple Inheritance

PronunciationMUL-ti-pul in-HAIR-i-tans

Definition

Multiple inheritance means a class has more than one parent class and inherits behaviour from all of them.

Where you hear it

In Python and C++ design discussions, explanations of mixins, and the diamond problem.

Examples

  • class D(B, C) inherits from both B and C.
  • Python uses the method resolution order to decide which parent's method runs first.
  • The Robot class inherits from both Walker and Talker, so it gets two sets of methods.

Common mistake

Using it to share code between unrelated classes. Prefer composition, or small mixins that add one behaviour.

Don't confuse with

Multilevel inheritance, a chain with one parent per level, such as A, then B, then C. Multiple inheritance has several direct parents.

Say it at work

  • In a meeting or chatMultiple inheritance makes the lookup order hard to see here.
  • In an email or ticketLet's use composition instead of a second parent class.