liveBook · Manning

Class hierarchy

As explained in The Well-Grounded Python Developer: How the pros use Python and Flask

A class hierarchy is an inheritance-oriented arrangement of classes in which related classes are organized into parent and child relationships.

A class hierarchy organizes related classes through parent–child relationships. Shared attributes and behavior live in a parent class, while child classes add or specialize features. section 4.1.3 section 4.3

The arrangement becomes useful when an application supports several related objects. A Shape class can manage common initialization and movement, while Rectangle, Square, and Circle provide their own drawing behavior. section 4.1.3

A practical hierarchy develops through a few clear relationships:

  • Rectangle inherits from Shape and overrides the empty draw() method with rectangle-specific rendering. section 4.1.3
  • Square inherits from Rectangle because a square is treated as a special rectangle whose height and width match. section 4.1.3
  • Circle inherits directly from Shape and supplies its own radius-based initialization and drawing behavior. section 4.1.3
  • Child constructors can call super() to reuse parent initialization while translating child-specific inputs, such as size, into the parent’s parameters. section 4.1.3

A hierarchy for onscreen shapes

Shape holds common state and behavior. Rectangle reuses that foundation and implements draw(). Square extends Rectangle with one size value, while Circle extends Shape with a radius. The application can then draw all three shape types while retaining shared management in Shape. section 4.1.3

How The Well-Grounded Python Developer: How the pros use Python and Flask covers it

How deep it goes
Explains inheritance through Shape subclasses, overrides, IS-A relationships, and super(), while stopping short of broader hierarchy design guidance.
Examples
Uses Shape, Rectangle, Square, and Circle classes to demonstrate shared state, specialized drawing, constructors, and onscreen rendering.
What it assumes
Assumes familiarity with Python classes and objects while teaching inheritance, parent–child relationships, and composition-based alternatives.
What you can do after
Build it from scratch by defining parent and child classes, overriding behavior, and reusing parent initialization with super().
How it connects
Connects inheritance with classes, encapsulation, namespaces, composition, code reuse, and the DRY principle.

Also explained in

Compare how 4 books explain class hierarchy →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Python Developer: How the pros use Python and Flask. The section links point to the parts of the book it draws on.