DEV Community

Cover image for Python basics - Day 25
Sabin Sim
Sabin Sim

Posted on

Python basics - Day 25

Day 25 – Method Overriding & super() Advanced

Project: Build a “Company Management App” that demonstrates advanced method overriding and the use of super() to extend parent behavior.


01. Learning Goal

By the end of this lesson, you will be able to:

  • Redefine parent methods through overriding
  • Extend parent behavior using super()
  • Combine inheritance and customization effectively
  • Write clean, maintainable OOP code with layered behavior

02. Problem Scenario

You’re designing a company management system where all employees share common behavior (work()),

but managers need to extend this logic with additional responsibilities.

This lesson focuses on how overriding and super() can help reuse and enhance parent functionality.


03. Step 1 – Method Overriding

A child class can redefine a method from its parent class.

The child’s method always takes priority.

class Animal:
    def speak(self):
        print("Generic animal sound.")

class Dog(Animal):
    def speak(self):  # Override
        print("Woof!")

d = Dog()
d.speak()   # Woof!
Enter fullscreen mode Exit fullscreen mode

04. Step 2 – Why Use Overriding

Overriding lets you customize behavior in each subclass
while still sharing the same structure defined in the parent class.


05. Step 3 – Using super()

super() allows the child class to call a method from the parent class.

class Person:
    def __init__(self, name):
        self.name = name

    def introduce(self):
        print(f"My name is {self.name}")

class Student(Person):
    def __init__(self, name, student_id):
        super().__init__(name)        # Call parent constructor
        self.student_id = student_id

    def introduce(self):
        super().introduce()           # Call parent method
        print(f"My student ID is {self.student_id}")

s = Student("Sabin", "S123")
s.introduce()
Enter fullscreen mode Exit fullscreen mode

Output:

My name is Sabin
My student ID is S123
Enter fullscreen mode Exit fullscreen mode

06. Step 4 – Extending Behavior with super()

Instead of completely replacing a parent method,
you can extend it with additional functionality.

class Logger:
    def log(self, message):
        print(f"Log: {message}")

class FileLogger(Logger):
    def log(self, message):
        super().log(message)  # Keep parent behavior
        print(f"(Saved to file) {message}")  # Add extra behavior

f = FileLogger()
f.log("System started")
Enter fullscreen mode Exit fullscreen mode

Output:

Log: System started
(Saved to file) System started
Enter fullscreen mode Exit fullscreen mode

07. Step 5 – Practice Examples

Example 1: Employee & Manager

class Employee:
    def __init__(self, name):
        self.name = name

    def work(self):
        print(f"{self.name} is working.")

class Manager(Employee):
    def work(self):
        super().work()  # Extend base behavior
        print(f"{self.name} is managing the team.")

m = Manager("Anna")
m.work()
Enter fullscreen mode Exit fullscreen mode

Output:

Anna is working.
Anna is managing the team.
Enter fullscreen mode Exit fullscreen mode

Example 2: Shape with Area

class Shape:
    def area(self):
        return 0

class Circle(Shape):
    def __init__(self, r):
        self.r = r

    def area(self):
        base = super().area()   # Extend parent method
        return base + 3.14 * self.r * self.r

c = Circle(5)
print("Circle area:", c.area())
Enter fullscreen mode Exit fullscreen mode

Output:

Circle area: 78.5
Enter fullscreen mode Exit fullscreen mode

08. Mini Project – Company Management App

Create a small system with multiple roles and extended behaviors.

class Employee:
    def __init__(self, name):
        self.name = name

    def work(self):
        print(f"{self.name} is performing general tasks.")

class Developer(Employee):
    def work(self):
        super().work()
        print(f"{self.name} is writing Python code.")

class Manager(Employee):
    def work(self):
        super().work()
        print(f"{self.name} is leading the project.")

employees = [Developer("Tom"), Manager("Anna")]
for e in employees:
    e.work()
Enter fullscreen mode Exit fullscreen mode

Output:

Tom is performing general tasks.
Tom is writing Python code.
Anna is performing general tasks.
Anna is leading the project.
Enter fullscreen mode Exit fullscreen mode

09. Reflection

You have learned how to:

  • Override parent methods to change or extend behavior
  • Use super() to reuse parent logic efficiently
  • Combine inheritance and extension to create flexible hierarchies
  • Apply OOP best practices to real-world systems

Next → Day 26 – Encapsulation & Access Control
Learn how to protect object data and manage accessibility using private attributes and getters/setters.

Top comments (0)