Method Overriding in Depth
A deeper look at how child classes redefine parent behavior -- the engine behind polymorphism.
Two Kinds of Overriding
Complete Override
The child completely replaces the parent's method. The parent's version is never called.
Partial Override (using super())
The child extends the parent's method by calling super() and then adding its own logic.
Complete Override: Shape Hierarchy
The parent Shape provides a default area() that returns 0. Each child replaces it entirely with its own calculation.
class Shape:
def area(self):
return 0
def describe(self):
return f"I am a shape with area {self.area()}"
class Circle(Shape):
def __init__(self, radius):
self.radius = radius
def area(self):
return 3.14159 * self.radius ** 2
class Rectangle(Shape):
def __init__(self, width, height):
self.width = width
self.height = height
def area(self):
return self.width * self.height
Polymorphism Through Overriding
Because both Circle and Rectangle override area(), we can loop through any list of shapes and call the same method. Each shape responds with its own calculation.
shapes = [Circle(5), Rectangle(4, 6), Circle(3)]
for shape in shapes:
print(shape.describe())
# I am a shape with area 78.53975
# I am a shape with area 24
# I am a shape with area 28.27431
Notice: The describe() method lives in the parent, but it calls self.area() -- which is the overridden version from each child. This is polymorphism at work.
Partial Override with super()
Sometimes you want to keep the parent's behavior and just add to it. Use super() to call the parent's method first, then add your own logic.
class Employee:
def __init__(self, name, salary):
self.name = name
self.salary = salary
def get_info(self):
return f"{self.name} earns ${self.salary}"
class Manager(Employee):
def __init__(self, name, salary, team_size):
super().__init__(name, salary)
self.team_size = team_size
def get_info(self):
base_info = super().get_info() # Keep parent's work
return f"{base_info} | Manages {self.team_size} people"
m = Manager("Alice", 95000, 12)
print(m.get_info())
# Alice earns $95000 | Manages 12 people
When to Use Which?
| Approach | When to Use |
|---|---|
| Complete Override | The child needs entirely different behavior (e.g., different area formula). |
| Partial Override | The child needs the parent's behavior plus something extra (e.g., extra info). |
| No Override | The parent's method already does exactly what the child needs. |
Best Practices
- Keep the same method signature (name and parameters) when overriding.
- Use
super()when you want to extend, not replace, the parent's logic. - Put shared/default behavior in the parent class so children only override what is different.
Common Mistakes
- Forgetting to call
super().__init__()in the child's constructor, causing missing attributes. - Changing the return type when overriding -- if the parent returns a number, the child should too.
- Overriding a method with different parameters, breaking the polymorphic contract.
Practice Exercises
Exercise 1: Triangle
Add a Triangle class to the Shape hierarchy above. Override area() to return 0.5 * base * height. Test it with the describe() method.
Exercise 2: Partial Override
Create an Animal base class with a info() method. Create Pet that uses super().info() and adds the owner's name.
Exercise 3: Polymorphic Loop
Create a list containing a Circle, Rectangle, and Triangle. Loop through and print each shape's area and description using the same method calls.
Summary
- Method overriding is the primary mechanism behind polymorphism in inheritance.
- Complete overrides replace the parent entirely; partial overrides use super() to extend.
- A parent method calling self.method() will use the child's overridden version.
- Keep method signatures consistent to maintain the polymorphic contract.