liveBook · Manning

Abstract class

As explained in Software Design for Python Programmers

An abstract superclass is a superclass intended to define shared structure or required behavior without being instantiated directly.

An abstract class defines shared structure and required behavior without allowing direct instantiation. In Python, it derives from abc.ABC and declares required methods with @abstractmethod. Concrete subclasses must implement every abstract method; otherwise, they remain abstract. The superclass may also provide instance variables and reusable non-abstract methods. section 4.3.4

MotorVehicle, Car, and Truck

MotorVehicle requires start_engine() and stop_engine(), while supplying drive(), accelerate(), steering, and braking behavior. Car implements the required engine methods and inherits the rest. Truck implements them and overrides steering. Calling drive() on either concrete object reuses the superclass sequence while preserving vehicle-specific behavior. section 4.3.4 section 4.7

Figure 8.3 This version of the application is modeled from the Template Method Design Pattern. The abstract superclass GameReport outlines the steps in the proper order for the algorithm to generate a report. It implements the common steps _print_header() and _print_footer() and delegates the remaining steps _acquire_data(), _analyze_data(), and _print_report() to the BaseballReport and VolleyballReport subclasses. The public template method generate_report() in the superclass calls the step methods in the proper order. The grayed-out portions of the diagram haven’t changed logically from figures 8.1 and 8.2.
A diagram of a sports report

AI-generated content may be incorrect.

Shows an abstract GameReport superclass fixing report-generation order, implementing common steps, and delegating variable steps to subclasses. section 8.1.3

How Software Design for Python Programmers covers it

How deep it goes
Explains Python implementation, instantiation rules, inheritance, overrides, and a Template Method application.
Examples
Uses MotorVehicle with Car and Truck, plus GameReport with baseball and volleyball report subclasses.
What it assumes
Assumes classes and inheritance, while introducing abstract methods, concrete subclasses, and interfaces.
What you can do after
Build abstract Python hierarchies that share implementation while enforcing subclass-specific methods.
How it connects
Connects abstract classes with inheritance, concrete subclasses, interfaces, and the Template Method design pattern.

Also explained in

Compare how 6 books explain abstract class →

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.