liveBook · Manning

Override

As explained in Software Design for Python Programmers

Method overriding is the definition of a subclass method with the same operation interface as an inherited superclass method, causing the subclass implementation to be selected for subclass instances.

Method overriding defines a subclass method with the same signature as an inherited superclass method. At run time, polymorphism selects the subclass implementation for subclass instances. Use it when the subclass needs similar, but not identical, behavior. section 7.1.1 section 7.6

Design an override so that it preserves the inherited interface and remains substitutable:

  • Match the method name, parameter count, parameter order, and return datatype; parameter names do not form part of the signature. section 7.1.1
  • Call super() when the subclass behavior extends the superclass behavior instead of replacing its core calculation. section 7.1.1
  • Keep subclass objects usable wherever superclass objects are expected; violating this substitution principle can cause logic errors or crashes. section 7.0 section 7.6
  • Treat type hints as documentation and checking aids, not runtime barriers: Python can still override a method with incompatible hinted types. section 7.1.2

Extending an item cost

class Item: def _cost(self, price, weight): return price * weight class OrganicItem(Item): def __init__(self, markup): self._markup = markup def _cost(self, price, weight): total = super()._cost(price, weight) return total * (1 + self._markup / 100) Item calculates the regular price, while OrganicItem overrides _cost() and adds a markup. Its print_cost() method can likewise provide organic-specific output. section 7.1.1

A misleading type-hint override

class Superclass: def operate(self, x: str, y: str): return x + y class Subclass(Superclass): def operate(self, x: int, y: int): return x * y Subclass.operate() overrides Superclass.operate() despite the incompatible hints. Calling sub.operate('Hi, ', 'Ron') therefore selects multiplication and raises a TypeError. section 7.1.2

How Software Design for Python Programmers covers it

How deep it goes
Explains signatures, runtime dispatch, super(), type-hint hazards, and contract concerns without descending into language-runtime internals.
Examples
Demonstrates Item and OrganicItem pricing, then exposes an incompatible operate() override through a TypeError.
What it assumes
Assumes basic inheritance and classes while developing signatures, polymorphism, and subclass design rules explicitly.
What you can do after
Apply it via Python subclass methods, reuse superclass behavior with super(), and diagnose incompatible overrides using runtime failures and type checking.
How it connects
Connects overriding with polymorphism, method overloading, type hints, the Liskov Substitution Principle, and programming by contract.

Also explained in

Compare how 3 books explain override →

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.