WIPIVERSE All articles

Inheritance (object-oriented programming)

Inheritance is a core mechanism in object-oriented programming (OOP) by which one class or object is based upon another class or object, retaining similar implementation and allowing for the reuse, extension, and modification of existing behavior. It is one of the primary characteristics of object-oriented programming, alongside encapsulation and polymorphism.

Definition and Basic Concepts

In class-based object-oriented programming, inheritance allows a new class (called a subclass, derived class, heir class, or child class) to acquire the properties and behaviors of an existing class (called a superclass, base class, or parent class). The subclass typically inherits the instance variables and member functions of its superclass while being able to add new members or override inherited ones. In most class-based languages, a derived class inherits all members of its base class except for constructors and finalizers, which must be defined by the derived class itself.

Inheritance establishes an incremental modification mechanism: a new class can be defined by declaring only those properties that differ from an existing class, while all other properties are automatically acquired from the parent. This is sometimes formalized as R = P ⊕ ΔR, where R is the new class, P the inherited properties, and ΔR the incrementally added properties that differentiate R from P.

The relationships created through inheritance form a hierarchy. In single inheritance, each class has one direct superclass, and the relationships form a tree structure. In multiple inheritance, a class can have more than one superclass, giving rise to a directed acyclic graph (DAG) rather than a tree.

History

The concepts underlying inheritance trace back to 1966, when Tony Hoare presented ideas about record subclasses. In 1967, Ole-Johan Dahl and Kristen Nygaard proposed a design allowing objects to belong to different classes while sharing common properties, with those common properties collected in a superclass. This idea was first implemented in the Simula 67 programming language. Inheritance subsequently spread to Smalltalk, C++, Java, Python, and many other languages. Simula's original mechanism was initially called "concatenation" (or "prefixing"); the more intuitive term "inheritance" was adopted later.

Types of Inheritance

Commonly distinguished types of inheritance include:

  • Single inheritance: A subclass inherits from exactly one superclass.
  • Multiple inheritance: A class has more than one superclass and inherits features from all of them.
  • Multilevel inheritance: A subclass is itself inherited from another subclass, forming a chain (e.g., class C derived from class B, which is derived from class A).
  • Hierarchical inheritance: One superclass serves as the base for more than one subclass.
  • Hybrid inheritance: A combination of two or more of the above types.

Inheritance versus Subtyping

Inheritance is distinct from subtyping. Subtyping establishes an is-a relationship and enables a given type to be substituted for another; inheritance, by contrast, may only reuse implementation and establish a syntactic relationship without necessarily ensuring behavioral compatibility. In some languages, particularly statically typed class-based OOP languages such as C++, C#, Java, and Scala, inheritance and subtyping coincide; in others they differ. To distinguish these concepts, subtyping is sometimes called interface inheritance, while inheritance of implementation is called implementation inheritance or code inheritance.

Inheritance is contrasted with object composition, which implements a has-a relationship (one object contains another). Some practitioners advocate "composition over inheritance," noting that implementation inheritance can introduce coupling problems.

Key Behaviors and Mechanisms

  • Overriding: Many languages allow a subclass to replace the implementation of an inherited behavior. Virtual methods enable dynamic dispatch of such invocations.
  • Visibility: The visibility of inherited members varies by language and derivation mode (e.g., private, protected, or public derivation in C++).
  • Non-subclassable classes: Some languages allow a class to be declared non-subclassable using modifiers such as final (Java, C++11+), sealed (C#), or frozen (Eiffel).

Issues and Criticism

Implementation inheritance has been controversial since at least the 1990s. Its critics include the authors of the Design Patterns book, who advocate interface inheritance and favor composition over inheritance. A central concern is the "fragile base class problem," in which modifications to a base class implementation can cause unintended behavioral changes in subclasses, reflecting the view that "inheritance breaks encapsulation." Additional noted problems include the "yo-yo problem" arising from complex layered hierarchies, and the static nature of inheritance, which does not easily support runtime changes to an object's behavior.

Inheritance should not be confused with the delegation used in prototype-based programming, in which one object delegates behavior to another rather than one class inheriting from another.

Browse

More topics to explore

    Browse all articles