liveBook · Manning

Inheritance

As explained in Software Design for Python Programmers

Inheritance is an object-oriented mechanism through which a subclass derives state and behavior from a superclass.

Inheritance creates subclasses from a superclass. The superclass passes down state through instance variables and behavior through methods; subclasses may add or override both. section 1.8

Use inheritance when a genuine is-a relationship supports a shared abstraction. A superclass can define common operations, while subclasses supply specialized behavior. Poorly chosen hierarchies, however, duplicate code, hardcode behavior, and spread changes across subclasses. section 7.3 section 7.3.1

A toy hierarchy with change leaks

A Toy superclass can require what(), play(), and sound(), with ToyCar, ModelAirplane, and TrainSet implementing them. The design duplicates play() and sound() implementations and hardcodes each toy's behavior. If play actions or sounds change, several subclasses may need modification. section 7.3.1

Inheritance also establishes a central design tradeoff:

  • Template Method uses inheritance: the superclass defines an algorithm outline and subclasses implement selected steps. section 8.3
  • Strategy uses composition instead: interchangeable algorithm objects remain loosely coupled to the client and can be added, removed, or updated later. section 8.3
  • Compare inheritance with aggregation when behaviors vary independently; favor the relationship that avoids duplication and hardcoded decisions. section 7.3 section 7.0
Figure 7.2 This version of the application uses is-a relationships between the Toy subclasses and their superclass. Two design faults are the duplicated code, which violates the Don’t Repeat Yourself Principle, and the hardcoded play action and sound behavior of each toy.
A diagram of a toy

Description automatically generated

Use the diagram to inspect the Toy superclass, its subclasses, and the duplicated or hardcoded behavior identified in the hierarchy. section 7.3.1

How Software Design for Python Programmers covers it

How deep it goes
Defines superclass and subclass inheritance, then examines duplication, hardcoding, overriding, composition, and design-pattern tradeoffs.
Examples
Demonstrates inheritance with book-catalogue classes, toy subclasses, and sports subclasses for players and venues.
What it assumes
Assumes classes, objects, instance variables, methods, and basic object-oriented programming vocabulary.
What you can do after
Evaluate inheritance against composition and select a hierarchy or pattern that limits duplication, coupling, and hardcoded behavior.
How it connects
Connects inheritance to object-oriented programming, polymorphism, encapsulation, abstraction, aggregation, composition, Template Method, Strategy, and the Liskov Substitution Principle.

Also explained in

Compare how 19 books explain inheritance →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Software Design for Python Programmers. The section links point to the parts of the book it draws on.