Python Method Overriding – With super() and Inheritance
Introduction – Why Method Overriding Matters
In object-oriented programming, method overriding lets you change or extend the behavior of methods defined in a parent class, by redefining them in a child class.
Python makes overriding incredibly intuitive, empowering you to:
- Customize behavior in subclasses
- Extend base class functionality using
super() - Implement polymorphism effectively
In this guide, you’ll learn:
- What method overriding is in Python
- Syntax and rules for overriding
- How to use
super()to extend base class logic - Real-world use cases
- Best practices and gotchas
What Is Method Overriding?
Method overriding allows a subclass to provide a specific implementation of a method already defined in its parent class.
Basic Example
class Animal:
def speak(self):
return "Some sound"
class Dog(Animal):
def speak(self):
return "Bark"
a = Animal()
print(a.speak()) # Some sound
d = Dog()
print(d.speak()) # Bark
The Dog class overrides the speak() method from Animal.
Why Override Methods?
- To customize or extend base class behavior
- To define subclass-specific logic
- To implement polymorphism
Using super() in Method Overriding
class Logger:
def log(self, message):
print(f"[LOG]: {message}")
class FileLogger(Logger):
def log(self, message):
super().log(message)
print("Writing to file:", message)
fl = FileLogger()
fl.log("Saving data")
Output:
[LOG]: Saving data
Writing to file: Saving data
super() lets you extend rather than replace the base method logic.
Rules for Method Overriding in Python
| Rule | Description |
|---|---|
| Method names must match | Same name and parameter list as the parent method |
| Happens in inheritance | Only works if subclass inherits from the parent |
| Overrides instance methods only | Not applicable to static or class methods directly |
super() accesses parent method | Used inside child method to invoke base method logic |
Real-World Example – Banking App
class Account:
def withdraw(self, amount):
print(f"Withdrawing ${amount} from account")
class SavingsAccount(Account):
def withdraw(self, amount):
if amount > 1000:
print("Withdrawal limit exceeded!")
else:
super().withdraw(amount)
s = SavingsAccount()
s.withdraw(500) # Allowed
s.withdraw(1500) # Limit exceeded
Common Mistakes
| Mistake | Fix |
|---|---|
| Forgetting to match method signature | Ensure same method name and parameters |
Forgetting super() when extending | Use super() if you want to include base logic |
| Overriding class/static method by mistake | Use @staticmethod and @classmethod carefully |
Best Practices
| Best Practice | Why It Matters |
|---|---|
| Always match method name and signature | Ensures correct override |
Use super() when extending behavior | Maintains DRY principle |
| Add docstrings in overridden methods | Increases readability and documentation |
| Don’t override unnecessarily | Avoid over-complication |
Summary – Recap & Next Steps
Python method overriding allows subclasses to redefine methods defined in parent classes, giving you the flexibility to customize or extend behavior.
Key Takeaways:
- Method overriding lets subclasses change parent method behavior
- Use
super()to reuse and extend the parent method - Always match method name and parameters when overriding
- It’s central to implementing polymorphism
Real-World Relevance:
Overriding is widely used in web frameworks, UI toolkits, file handlers, and data processing pipelines.
FAQ – Python Method Overriding
What is method overriding?
Redefining a method in a subclass that already exists in the parent class.
When should I use super()?
Use super() when you want to run the base class method in addition to or before your subclass’s method logic.
Can I override static or class methods?
Yes, but it’s not method overriding in the traditional sense—just redefining them in a subclass. Use with care.
Does Python support method overloading?
Not directly. You can simulate it using default parameters or *args.
What happens if I don’t override a method?
The subclass will inherit the parent method and use it as-is.
Share Now :
