Inheritance

In object-oriented programming (OOP), Inheritance is a way to compartmentalize and reuse code by creating collections of attributes and behaviors called objects which can be based on previously created objects. In classical inheritance where objects are defined by classes, classes can inherit other classes. The new classes, known as Sub-classes (or derived classes), inherit attributes and behavior of the pre-existing classes, which are referred to as Super-classes (or ancestor classes). The inheritance relationship of sub- and superclasses gives rise to a hierarchy. In Prototype-based programming objects can be defined directly from other objects without the need to define any classes.

The inheritance concept was invented in 1967 for Simula.


Inheritance should not be confused with (subtype) polymorphism, commonly called just polymorphism in object-oriented programming. Inheritance is a relationship between implementations, whereas subtype polymorphism is relationship between types (interfaces in OOP). (Compare connotation/denotation.) In some, but not all OOP languages, the notions coincide because the only way to declare a subtype is to define a new class that inherits the implementation of another.

Inheritance does not entail behavioral subtyping either. It is entirely possible to derive a class whose object will behave incorrectly when used in a context where the parent class is expected; see the Liskov substitution principle.

Complex inheritance, or inheritance used within an insufficiently mature design, may lead to the Yo-yo problem.