liveBook · Manning

Abstract method

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

An abstract method is a method declaration that specifies an operation for subclasses without providing its implementation.

An abstract method defines an operation that subclasses must provide without implementing the operation itself. It belongs in a shared parent class when every child should perform the same responsibility differently. section 4.1.3

A practical inheritance design uses an abstract method to:

  • Keep shared attributes and behavior in a parent class such as Shape. section 4.1.3
  • Require each concrete child, such as Rectangle or Circle, to implement its own draw() method. section 4.1.3
  • Support additional shapes while keeping the application’s common structure maintainable. section 4.1.3

Shapes that draw themselves

Shape declares draw() but does not provide its functionality. Rectangle inherits from Shape and overrides draw() with rectangle-rendering code; Circle supplies a different implementation. Shape should not be instantiated directly, while concrete shapes can be drawn by the application. section 4.1.3

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

How deep it goes
Explains abstract declarations, non-instantiation, subclass implementation, and method overriding through a shape hierarchy.
Examples
Uses Shape, Rectangle, Square, and Circle classes in an onscreen drawing application.
What it assumes
Assumes familiarity with classes while demonstrating inheritance, parent initialization, and subclass method replacement.
What you can do after
Build it from scratch by separating shared shape behavior from subclass-specific drawing implementations.
How it connects
Connects abstract methods with inheritance, abstract classes, subclasses, method overriding, and shared parent behavior.

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 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.