Overriding a Method in a Subclass
Introduction
Welcome back to Sharing Behavior Between Classes in Python. We are now beginning Unit 3 of 3. In the previous lesson, we built a subclass that inherited a parent's constructor and methods, then added its own behavior on top. Inheritance gave us reuse — but sometimes a subclass needs to replace or extend a method the parent already defines. That is called method overriding.
In this lesson, we will learn how to:
- Override a base-class method so the subclass version runs instead of the parent's.
- Call
super()inside an overridden method to reuse the parent's result and add subclass-specific detail. - Write a second subclass that overrides the same method in a different way.
- Store a base instance and several subclass instances in one list and call the shared method on each.
By the end, we will have a small fleet where every vehicle answers describe() in its own voice.
What Method Overriding Means
When a subclass defines a method with the same name as one on the parent, the subclass version overrides the parent's version for objects of that subclass. From outside, we still call describe() — but which implementation runs depends on the object's actual type.
Our base Vehicle class provides a general description:
Subclasses can replace this behavior entirely, or build on it. We will see both approaches.
Extend the Parent's Result with super()
An electric car is a vehicle, so its description should still mention the model and wheels — but it should also mention electric range. Instead of rewriting the parent's logic, we call the parent's describe() through super() and append our extra detail:
Inside describe(), super().describe() runs the parent's version on the same object and returns "Volt with 4 wheels". We wrap that string with the range information. This is overriding with extension: the subclass specializes the method without throwing away the parent's work.
