1. Introduction: Beyond Basic Inheritance
In the study of Object-Oriented Programming (OOP), inheritance allows a child class to inherit and reuse all data attributes and methods defined in a parent (base) class. In previous examples, an Animal parent class defined a default speak() method, and a Dog child class inherited that method directly.
While inheriting general behaviors saves time, real-world objects often require specialized behaviors rather than generic defaults. Python provides a mechanism that allows an inherited class to redefine an existing method from its parent class with a new definition tailored to the child class. This capability is known as Method Overriding.
2. What Is Method Overriding?
Method Overriding is an OOP feature where a child (derived) class provides its own specific implementation of a method that is already defined in its parent class.
- The parent method: Defines a generic or default behavior (for example, general animals producing "some sound").
- The overriding method: Maintains the exact same method name (
speak) inside the child class, but provides a new block of code tailored specifically to that entity (such as a dog producing "woof woof").
- When the method is invoked on an object of the child class, Python executes the child's redefined version, completely overriding the parent class implementation.
3. Why Override Instead of Creating a New Method?
A common question arises when designing subclasses: Why not simply create a brand-new method with a different name (such as bark()) to print "woof woof"?
- Avoiding redundant methods: If you define an additional method like
bark(), the child class ends up with two separate sound-producing functions: the inherited speak() from Animal and the new bark() in Dog.
- Clean class interfaces: In software design, a dog does not need two separate functions to produce sounds. The goal is for the
Dog class to have a single, unified speak() function that produces the correct dog sound, cleanly overriding the generic parent implementation.
4. Step-by-Step Code Implementation in Jupyter Notebook
Step 1: Defining the Parent Class (Animal)
First, define the Animal base class containing a generic speak() method:
# Parent (Base) Class
class Animal:
def speak(self):
print("some sound")
When executed, this establishes the parent class template in memory.
Step 2: Defining the Child Class with Method Overriding (Dog)
Next, define the Dog class, inherit from Animal by placing it inside parentheses, and redefine the speak() method with dog-specific behavior:
# Child (Derived) Class inheriting from Animal
class Dog(Animal):
# Overriding the parent's speak() method with a new definition
def speak(self):
print("woof woof")
Both class templates are now compiled and ready for instantiation.
5. Instantiating Objects and Verifying Execution
To observe method overriding in action, instantiate an object from the Dog class and call speak():
# 1. Instantiating an object of the child class
dog1 = Dog()
# 2. Invoking the overridden speak method
dog1.speak()
Execution Output:
woof woof
Execution Walkthrough
- Even though
Dog inherits from Animal, calling dog1.speak() does not execute the parent class's speak() method (which outputs "some sound").
- Instead, Python identifies that
Dog provides its own local definition of speak(), executing it directly and outputting "woof woof".
- The parent class method is successfully overridden inside the inherited class.