Skip to content

Questions about Inheritance (object-oriented programming)

Short answers, pulled from the story.

What is inheritance in object-oriented programming?

Inheritance is the mechanism of basing a class upon another class, retaining similar implementation. A child class acquires all the properties and behaviors of its parent class, with the exception of constructors, destructors, overloaded operators, and friend functions of the base class. The relationships formed through inheritance produce a directed acyclic graph.

Who invented inheritance in object-oriented programming?

Ole-Johan Dahl and Kristen Nygaard presented the foundational design in 1967, influenced by Tony Hoare's 1966 remarks on record subclasses. Their idea was first adopted in the Simula 67 programming language and later spread to Smalltalk, C++, Java, Python, and many other languages.

What are the different types of inheritance in OOP?

The main types are single inheritance, where a subclass inherits from one superclass; multiple inheritance, where a class inherits from more than one parent; multilevel inheritance, where a subclass is itself derived from another subclass; hierarchical inheritance, where one base class has more than one subclass; and hybrid inheritance, which combines two or more of these patterns.

What is the fragile base class problem in inheritance?

The fragile base class problem, identified by Allen Holub, is that modifications to a base class can cause inadvertent behavioral changes in subclasses. Because subclasses share the implementation of their parent, changes in the parent propagate through the hierarchy unpredictably. Holub summarized this as inheritance breaking encapsulation.

What is the difference between inheritance and subtyping?

Subtyping establishes an is-a relationship, meaning a subtype can be substituted for another type or abstraction. Inheritance reuses implementation and establishes a syntactic relationship, not necessarily a semantic one. A derived class whose object behaves incorrectly when used in a context where the parent class is expected violates the Liskov substitution principle.

What are the alternatives to inheritance in object-oriented design?

The composite reuse principle separates behaviors from the primary class hierarchy and includes specific behavior classes as needed, allowing runtime modification. Explicit delegation requires more code but avoids the substitutability issues of implementation inheritance. The decorator pattern addresses the static nature of inheritance, and role-oriented programming introduces a distinct played-by relationship combining properties of both inheritance and composition.