Lably
Skip to content

Multiple inheritance

In software, to have several parent classes

  • 10 min read
  • 3 chapters

Related on Lably

Through Object-oriented programming, Class (programming) and Inheritance (object-oriented programming)

Multiple inheritance has been a controversial issue for many years, with opponents pointing to its increased complexity and ambiguity in situations such as the "diamond problem", where it may be ambiguous as to which parent class a particular feature is inherited from if more than one parent class implements said feature. This can be addressed in various ways, including using virtual inheritance. Alternate methods of object composition not based on inheritance such as mixins and traits have also been proposed to address the ambiguity.

Details

In object-oriented programming (OOP), inheritance describes a relationship between two classes in which one class (the child class) subclasses (or inherits from or extends) the parent class. The child inherits methods and attributes 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 child class Cat that inherits those features without having to explicitly program them, while adding new features like chasing mice.

Multiple inheritance allows programmers to use more than one totally orthogonal hierarchy simultaneously, such as allowing Cat to extend CartoonCharacter, Pet and Mammal and access features from within all of those classes.

Sign in to keep reading

2 more chapters are free with a Lably account.

More in Computing and communication