method overriding
Method overriding is a fundamental concept in object-oriented programming (OOP) that allows you to redefine a method in a subclass when that method was already defined in the base class.
This technique is useful when you want to provide a specific implementation for a method that’s already defined in the parent class. By overriding a method, you ensure that the subclass’s version is used instead of the superclass’s version, which allows you to customize or extend the behavior of the parent class’s method in the subclass.
When you call an overridden method on an object, Python picks the version to run from the object’s actual class at run time, a behavior called dynamic dispatch. This makes overriding the engine behind polymorphism, since code written against the base class automatically picks up the subclass version.
To watch that selection happen, pick an object’s actual class below and follow dynamic dispatch as it climbs the inheritance chain to the first class that overrides the method:
Method overriding is distinct from overloading, where one name maps to several methods that differ in their parameters. Python doesn’t support overloading by signature, but other languages mark overriding in their own way. For example, Java provides the @Override annotation as a compile-time check, and C++ dispatches dynamically only for methods declared virtual.
Since Python 3.12, you can mark an overriding method with the @typing.override decorator so that static type checkers flag it if it doesn’t actually override anything in a base class. Python doesn’t enforce this at run time.
Example
Here’s a quick example to illustrate method overriding in Python:
animals.py
class Animal:
def speak(self):
print("Animal speaks")
class Dog(Animal):
def speak(self):
print("Woof Woof")
# Usage
animal = Animal()
animal.speak() # Output: Animal speaks
dog = Dog()
dog.speak() # Output: Woof Woof
In this example, the .speak() method in the Dog class overrides the .speak() method in the Animal class. When you call .speak() on an instance of Dog, it uses the overridden method in the Dog class.
You can also use super() to extend rather than fully replace the parent method:
class Dog(Animal):
def speak(self):
super().speak()
print("Woof Woof")
Related Resources
Tutorial
Inheritance and Composition: A Python OOP Guide
Learn how inheritance and composition in Python model is-a and has-a relationships, and when to use each to design flexible object-oriented code.
For additional information on related topics, take a look at the following resources:
- Supercharge Your Classes With Python super() (Course)
- Inheritance and Internals: Object-Oriented Programming in Python (Course)
- Object-Oriented Programming (OOP) in Python (Tutorial)
- Python Classes: The Power of Object-Oriented Programming (Tutorial)
- Object-Oriented Programming in Python vs Java (Tutorial)
- Inheritance and Composition: A Python OOP Guide (Course)
- Inheritance and Composition: A Python OOP Guide (Quiz)
- Intro to Object-Oriented Programming (OOP) in Python (Course)
- A Conceptual Primer on OOP in Python (Course)
- Object-Oriented Programming (OOP) in Python (Quiz)
- Class Concepts: Object-Oriented Programming in Python (Course)
- Python Classes - The Power of Object-Oriented Programming (Quiz)
- Python vs Java: Object Oriented Programming (Course)
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.