liveBook · Manning

Abstract method

As explained in Software Design for Python Programmers

The abstractmethod decorator marks a method as abstract so that concrete subclasses must implement it.

The @abstractmethod decorator marks a method that concrete subclasses must implement. An abstract class may also provide shared state and concrete methods, while a subclass missing any required implementation remains abstract. section 4.3.4

Vehicle contract with shared behavior

MotorVehicle declares start_engine() and stop_engine() with pass, then supplies drive() and other shared operations. Car and Truck implement the abstract methods; Truck additionally overrides turning behavior. MotorVehicle cannot be instantiated, but Car and Truck can be driven through the inherited workflow.

Abstract methods can define required algorithms, but placing duplicated algorithms in subclasses creates duplication, hardcoded behavior, and change leaks. The sports example motivates separating reusable algorithm families through the Strategy Design Pattern. section 8.2.2

How Software Design for Python Programmers covers it

How deep it goes
Explains decoration, instantiation restrictions, inheritance, and duplicated algorithm drawbacks through worked Python examples.
Examples
Uses MotorVehicle, Car, Truck, and sports classes to demonstrate required methods, shared workflows, overrides, and duplicated algorithms.
What it assumes
Assumes familiarity with Python classes and inheritance while defining abstract classes and concrete subclasses explicitly.
What you can do after
Build it from scratch by declaring abstract methods with @abstractmethod and implementing every requirement in concrete subclasses.
How it connects
Connects abstract methods with abstract classes, concrete subclasses, interface classes, inheritance, and the Strategy Design Pattern.

Also explained in

Compare how 2 books explain abstract method →

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.