Method overriding in Python is an object-oriented programming concept that allows a child class to provide its own implementation of a method that is already defined in its parent class. It is mainly used with inheritance when the child class needs to perform the same operation in a different way. Method overriding helps achieve runtime polymorphism by allowing the same method name to perform different actions depending on the object that calls it. For example, a parent Animal class can have a sound() method, while child classes such as Dog and Cat can override this method to provide their own behavior.
Table of Contents
What is Method Overriding in Python?
Method overriding occurs when a child class defines a method with the same name as a method in its parent class. When the method is called using an object of the child class, the child class's implementation is executed instead of the parent's implementation. Method overriding is possible because of inheritance in Python.
Example:
# Python program to demonstrate method overriding
class Animal:
def sound(self):
print("Animal makes a sound")
class Dog(Animal):
def sound(self):
print("Dog barks")
dog = Dog()
dog.sound()
Output:
Dog barks
Explanation:
- Animal is the parent class and contains the sound() method.
- Dog inherits from the Animal class.
- The Dog class defines its own sound() method.
- The sound() method in Dog overrides the method inherited from Animal.
- When dog.sound() is called, the Dog implementation is executed.
Why Use Method Overriding in Python?
Method overriding is useful when different child classes need to provide different behavior for the same method.
- Provides different behavior: Child classes can define their own implementation of a parent method.
- Supports runtime polymorphism: The same method call can produce different results depending on the object.
- Improves code flexibility: A common method name can be used while allowing different implementations.
- Promotes code reuse: Common functionality can remain in the parent class while specialized behavior is implemented in child classes.
- Makes programs easier to extend: New child classes can override existing methods without changing the parent class.

How Method Overriding Works in Python?
Method overriding works when a child class inherits a method from a parent class and defines a method with the same name. Python uses the method defined in the child class when the method is called on a child object.
Example:
# Python program to understand how method overriding works
class Vehicle:
def start(self):
print("Vehicle starts")
class Car(Vehicle):
def start(self):
print("Car starts with a key")
car = Car()
car.start()
Output:
Car starts with a key
Explanation:
- Vehicle defines the start() method.
- Car inherits from Vehicle.
- Car defines another start() method with the same name.
- The Car implementation overrides the parent implementation.
- Calling car.start() executes the method defined in Car.
Method Overriding with Inheritance
Method overriding is closely related to inheritance. A child class inherits methods from its parent class and can override an inherited method by defining a method with the same name.
Syntax:
class Parent:
def method_name(self):
# Parent implementation
class Child(Parent):
def method_name(self):
# Child implementation
Example:
# Python program to demonstrate method overriding with inheritance
class Employee:
def work(self):
print("Employee is working")
class Developer(Employee):
def work(self):
print("Developer is writing code")
developer = Developer()
developer.work()
Output:
Developer is writing code
Explanation:
- Employee contains the work() method.
- Developer inherits from Employee.
- Developer defines its own work() method.
- The child method overrides the parent method.
- Calling developer.work() executes the child implementation.
Method Overriding with the super() Function
The super() function is used to access methods and attributes from a parent class. It can be used inside an overridden method when the child class wants to reuse the parent's implementation and then add its own behavior.
Syntax:
class Child(Parent):
def method_name(self):
super().method_name()
# Additional child implementation
Example:
# Python program to demonstrate method overriding using super()
class Animal:
def sound(self):
print("Animal makes a sound")
class Dog(Animal):
def sound(self):
super().sound()
print("Dog barks")
dog = Dog()
dog.sound()
Output:
Animal makes a sound
Dog barks
Explanation:
- Animal defines the sound() method.
- Dog overrides the sound() method.
- super().sound() calls the parent class implementation.
- The Dog class then performs its additional operation.
- This allows the child class to extend the behavior of the parent method.
Method Overriding with Multiple Child Classes
Multiple child classes can override the same method from a parent class. Each child class can provide a different implementation according to its requirements.
Example:
# Python program to demonstrate method overriding
# with multiple child classes
class Animal:
def sound(self):
print("Animal makes a sound")
class Dog(Animal):
def sound(self):
print("Dog barks")
class Cat(Animal):
def sound(self):
print("Cat meows")
dog = Dog()
cat = Cat()
dog.sound()
cat.sound()
Output:
Dog barks
Cat meows
Explanation:
- Animal contains the common sound() method.
- Dog overrides sound() and provides its own implementation.
- Cat also overrides sound() with a different implementation.
- The same method name performs different actions.
- This is a common example of runtime polymorphism.
Method Overriding and Runtime Polymorphism
Method overriding is an important mechanism used to achieve runtime polymorphism. Runtime polymorphism means that the method that gets executed depends on the object being used at runtime.
Example:
# Python program to demonstrate runtime polymorphism
# using method overriding
class Animal:
def sound(self):
print("Animal makes a sound")
class Dog(Animal):
def sound(self):
print("Dog barks")
class Cat(Animal):
def sound(self):
print("Cat meows")
animals = [Dog(), Cat()]
for animal in animals:
animal.sound()
Output:
Dog barks
Cat meows
Explanation:
- Animal defines the common sound() method.
- Dog and Cat override the method.
- Both objects are stored in the same list.
- The for loop calls sound() on each object.
- Python executes the appropriate child-class implementation at runtime.
Method Overriding with Different Arguments
When overriding a method, the child class should generally provide a compatible method interface with the parent method. The method name remains the same while the implementation changes.
Example:
# Python program to demonstrate method overriding
# with different implementations
class Calculator:
def calculate(self, a, b):
print("Addition:", a + b)
class AdvancedCalculator(Calculator):
def calculate(self, a, b):
print("Multiplication:", a * b)
calculator = AdvancedCalculator()
calculator.calculate(5, 4)
Output:
Multiplication: 20
Explanation:
- Calculator defines the calculate() method.
- AdvancedCalculator inherits from Calculator.
- The child class defines another calculate() method.
- The child implementation overrides the parent implementation.
- The same method can therefore perform a different operation.
Note:
Method overriding is about replacing or extending inherited behavior. Python does not require the child method to have exactly the same parameter list, but keeping a compatible interface is generally better for maintainable object-oriented code.
Advantages of Method Overriding
Method overriding provides several advantages when designing object-oriented Python programs.
- Supports polymorphism: Different objects can respond differently to the same method call.
- Allows customization: Child classes can customize inherited behavior according to their requirements.
- Improves flexibility: A common interface can be used for different implementations.
- Promotes code reuse: Parent classes can contain common functionality that child classes can reuse or extend.
- Makes applications easier to extend: New child classes can provide their own behavior without modifying existing parent classes.
Common Beginner Mistakes
1. Forgetting to Use Inheritance: Method overriding normally requires a parent-child relationship created through inheritance.
Wrong:
# Python program showing a class without inheritance
class Animal:
def sound(self):
print("Animal makes a sound")class Dog:
def sound(self):
print("Dog barks")
Correct:
# Python program showing method overriding using inheritance
class Animal:
def sound(self):
print("Animal makes a sound")
class Dog(Animal):
def sound(self):
print("Dog barks")
2. Using a Different Method Name: A method must have the same name as the inherited method to override it.
Wrong:
# Python program showing an incorrect method name
class Animal:
def sound(self):
print("Animal makes a sound")class Dog(Animal):
def bark(self):
print("Dog barks")
Correct:
# Python program showing the correct method name for overriding
class Animal:
def sound(self):
print("Animal makes a sound")class Dog(Animal):
def sound(self):
print("Dog barks")
3. Forgetting self: Instance methods need self as the first parameter.
Wrong:
# Python program showing a missing self parameter
class Animal:
def sound():
print("Animal makes a sound")class Dog(Animal):
def sound():
print("Dog barks")
Correct:
# Python program showing the correct use of self
class Animal:
def sound(self):
print("Animal makes a sound")class Dog(Animal):
def sound(self):
print("Dog barks")
4. Forgetting super() When Parent Behavior Is Required: If the child method completely replaces the parent method, the parent implementation will not run automatically. Use super() when the child class also needs the parent behavior.
Example:
# Python program to use super() with method overriding
class Animal:
def sound(self):
print("Animal makes a sound")class Dog(Animal):
def sound(self):
super().sound()
print("Dog barks")dog = Dog()
dog.sound()
Output:
Animal makes a sound
Dog barks
5. Confusing Method Overriding with Method Overloading: Method overriding occurs between a parent class and a child class, while method overloading refers to using the same method name with different parameter patterns. Python does not support traditional method overloading in the same way as languages such as Java.
Example of overriding:
# Python program to demonstrate method overriding
class Parent:
def show(self):
print("Parent method")class Child(Parent):
def show(self):
print("Child method")obj = Child()
obj.show()
Output:
Child method
Conclusion
Method overriding is an important concept in Python object-oriented programming that allows a child class to provide its own implementation of a method inherited from a parent class. It is mainly used with inheritance and is an important mechanism for achieving runtime polymorphism.
By using method overriding, different child classes can provide different behaviors while following a common interface. The super() function can also be used when the child class needs to reuse or extend the behavior of the parent class. Understanding method overriding makes it easier to design flexible, reusable, and maintainable Python applications.
Frequently Asked Questions (FAQs)
1. What is method overriding in Python?
Method overriding is the process in which a child class provides its own implementation of a method that is already defined in its parent class.
2. Why is method overriding used in Python?
Method overriding is used to provide different implementations of inherited methods and to support runtime polymorphism.
3. Is method overriding possible without inheritance?
No. Method overriding normally requires a parent-child relationship created through inheritance.
4. What is the role of super() in method overriding?
The super() function is used to access the parent class implementation from an overridden method in the child class.
5. What is the difference between method overriding and method overloading?
Method overriding occurs when a child class redefines a method inherited from a parent class. Method overloading refers to using the same method name with different parameter patterns. Python does not support traditional compile-time method overloading like Java.
0 Comments