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.
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
Object-Oriented Programming (OOP) in Python
Learn object-oriented programming in Python: the OOP paradigm, classes and inheritance, and how to instantiate objects from a class.
For additional information on related topics, take a look at the following resources:
- Python Classes: The Power of Object-Oriented Programming (Tutorial)
- Inheritance and Composition: A Python OOP Guide (Tutorial)
- Supercharge Your Classes With Python super() (Course)
- Inheritance and Internals: Object-Oriented Programming in Python (Course)
- Object-Oriented Programming in Python vs Java (Tutorial)
- A Conceptual Primer on OOP in Python (Course)
- Intro to Object-Oriented Programming (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)
- Inheritance and Composition: A Python OOP Guide (Course)
- Inheritance and Composition: A Python OOP Guide (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.