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 is 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.
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.
Related Resources
Tutorial
Object-Oriented Programming (OOP) in Python
In this tutorial, you'll learn all about object-oriented programming (OOP) in Python. You'll learn the basics of the OOP paradigm and cover concepts like classes and inheritance. You'll also see how to instantiate an object 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)
- Intro to Object-Oriented Programming (OOP) in Python (Course)
- Object-Oriented Programming (OOP) in Python (Quiz)
- Class Concepts: Object-Oriented Programming in Python (Course)
- Inheritance and Internals: 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)
By Leodanis Pozo Ramos • Updated May 6, 2025