Method overriding in Python allows a child class to replace or modify a method defined in the parent class. When a method is called using a child object, Python runs the child’s version instead of the parent’s. This makes the codebase more flexible, reusable, and maintainable. This is also a primary example of run-time polymorphism. In this blog, you will learn about method overriding in Python, including detailed examples.
Table of Contents:
What is Method Overriding in Python?
Method overriding in Python allows a child class to replace or modify a method defined in the parent class. By doing this, the child class can change how the method works or add new behavior to it. This feature is useful when the parent’s method is not enough, and you want the child class to handle things in its own way.
Importance and Benefits of Method Overriding
1. Customization: Child classes may provide their implementation of a method based on their requirements, while still having the parent class as a foundation.
2. Reusability: The shared logic is maintained in the parent class, and only that which is necessary is updated in the child class without code duplication.
3. Flexibility: The method can be implemented differently depending on the child’s class.
4. Clean Code: Overriding avoids creating multiple method names for similar functionality, making the code simpler.
5. Real-World Use: It is possible to have a general course information method in the parent
class, and child classes, such as DataScienceCourse or PythonCourse, override it to provide
their own information, such as duration, course level, or course topics.
Unlock Python: Supercharge Your Coding Skills!
Practice coding with hands-on projects and examples to quickly level up your Python skills.
Rules of Method Overriding in Python
When overriding a method in Python, there are a few simple rules that should be followed to
ensure the smooth execution of the program:
- Same Method Name: The child class method should have the same name as the parent class method.
- Same Parameters: The child class should use the same parameters as the parent class. It can add more logic inside the method, but the number and type of parameters stay the same.
- Inheritance is Compulsory: Method overriding only applies to the child and parent classes
where the child class inherits the parent class.
- Priority of Child Class: When invoking the method with the child class object, Python will
always execute the child class version, even if the parent class has the same method.
- Use of super(): This means that the child class can still access the parent class version by
using the super() function.
How Method Overriding Works in Python
Method overriding in Python takes place through a series of steps. First, a method is written in
the parent class, and then the child class will use its version. When the same method is called
from the parent and child classes, Python will be responsible for deciding which one it will call at
run-time. Let’s explore the steps in detail:
Step 1: Define the Class
We will start by creating a parent class and a parent class method, and then a child class that inherits the parent class and overrides the method.
Example:
Explanation: Here, the child class DataScienceCourse is overriding the method course_info of its parent class.
Step 2: Method Lookup and Resolution
When a method is invoked, Python first checks to see if the method is inside the child class. It will use the child class version if it exists. Otherwise, Python will search for the method in the parent class.
Example:
Output:
Explanation: Here, Python finds the method in the child class, so the version of the parent class is not used.
Step 3: Create Objects and Run the Methods at Runtime
When objects are created from both the parent and the child classes, at runtime, Python will determine which method to call.
Example:
Output:
Explanation: Here, the parent class object p calls the parent version of the method, and the child class object c calls the overridden version. Again, Python decides at runtime, not as we write the code.
Step 4: Accessing Parent Methods with super()
There may be times when the child class needs the parent class’s method and its own logic. In those cases, super() is used.
Example:
Output:
Explanation: Here, the super() function allows the child class to first access the parent method, and then add its own functionality
Get 100% Hike!
Master Most in Demand Skills Now!
Examples of Method Overriding in Python
The examples below show how method overriding works in Python and how the child classes can override the parent class methods.
1. Using Parent Class Name in Python
In Python, a child class can call the parent class method not only by using super(), but also by calling the parent class method directly. Calling it directly can be useful when you need to be very clear about which parent class method is being called, and in cases of multiple inheritance.
Output:
Explanation: Here, the parent method Course.course_info(self) is directly called, and the child class adds its own information. This will allow both parent and child methods to cooperate while leaving the overridden behavior intact.
2. Using super() Function in Python
In Python, the super() function allows a child class to invoke methods in its parent class without referring explicitly to the parent class. This is especially useful in method overriding because it allows the child class to extend or reuse the parent’s behavior.
Output:
Explanation: Here, the super() function is used to invoke the parent class method course_info(). The child class then adds its own information, so that both the parent and the child methods contribute to the result.
Note: Calling the parent class directly is less flexible in multiple inheritance. Using super() is better because it automatically follows the method resolution order.
3. Overriding with Arguments in Python
In Python, method overriding can apply to methods that take arguments. The child class can write the same method with the same parameters, but can change its code. This makes the method more flexible and better suited for the child class.
Example:
Output:
Explanation: Here, both the parent and child classes define the same method, course_info(duration), with an argument. The parent class provides a general message. The child class overrides the method to add additional specific details about projects. The method called depends on which object is used.
Method Overriding with Multiple Inheritance in Python
In Python, a class can inherit from more than one parent class. This is called multiple inheritance. When a method is present in more than one parent class, the child class can override it to provide its own version. Python uses the Method Resolution Order (MRO) to decide which parent’s method to use if the child class does not override it.
Example:
Output:
Explanation: Here, both parent classes have the same method, but the child class provides its own version. Python uses the child’s method when it is called.
Difference Between Method Overloading and Method Overriding in Python
Feature |
Method Overloading in Python |
Method Overriding in Python |
Definition |
Same method name handles different inputs using default arguments, *args, **kwargs, or modules like multipledispatch. |
Same method name in parent and child classes, but child changes the method’s behavior. |
Class Location |
Defined inside the same class with different input handling. |
Defined in a child class that inherits from a parent class. |
Execution Time |
In other languages, resolved at compile time. In Python, simulated at runtime. |
Always resolved at runtime, also called runtime polymorphism. |
Purpose |
Lets one method handle many inputs, avoiding multiple method names. |
Lets the child class provide its own version of a method for flexibility. |
Polymorphism Type |
Overloading polymorphism (one method, different inputs). |
Overriding polymorphism (child replaces parent’s method). |
Limitations of Method Overriding in Python
1. Strong Dependency: The child class depends too much on the parent class, which reduces the dependency.
2. Parent Method Ignored: The original parent method can be ignored or fully overridden.
3. Less Clarity: Too many methods are overridden, which makes it very difficult to know which one is running.
4. Hard to Debug: Tracking errors is very difficult when there are multiple classes overridden in the same method.
5. Risk of Breakage: Even a small change to the methods of the parent class may break the child class methods.
Real-world Applications of Method Overriding in Python
1. Customizing Course Details: Different types of course classes can use the same method for showing the information, but each class can provide its own details, like duration, level, or projects.
2. Payment Processing in E-commerce: Various payment classes can use a similar method name but handle the processing in their own way for credit cards, UPI, or digital wallets.
3. Notification System: The same method like send_notification() can be defined in multiple classes, and each class overrides it to send the message through email, SMS, or mobile app alerts.
4. Bank Transactions: Different classes like the SavingsAccount, CurrentAccount, or the LoanAccount can override the same method called the calculate_interest() for applying their own rules during the calculation of the interest.
5. User Authentication in Applications: Different classes like Teacher, Admin, and Students can override the same method login(), but each class applies its own set of rules for verifying and controlling access.
Start Coding in Python for Free: No Experience Needed!
Begin writing real Python code through interactive, beginner-friendly modules completely for free.
Conclusion
Method overriding in Python is a key concept of object-oriented programming that helps in making the code more flexible. It allows the child classes to modify the behavior of the parent class methods, which makes the program easier to maintain and supports runtime polymorphism. This approach also helps in improving the reusability and allows the developers to extend functionality without rewriting the existing code.
Take your skills to the next level by enrolling in the Python Course today and gaining hands-on experience. Also, prepare for job interviews with Python Interview Questions prepared by industry experts.
Method Overriding in Python – FAQs
Q1. What is method overriding in Python?
Method overriding in Python lets a child class change or extend a method defined in its parent class.
Q2. How is method overriding different from method overloading in Python?
Overriding changes a parent method in the child class, while overloading handles different inputs in the same class.
Q3. Why use method overriding in Python?
It makes code flexible, reusable, and allows child classes to customize parent behavior.
Q4. Can a child class access the parent method when overriding?
Yes, using super() or calling the parent class directly allows access to the original method.
Q5. Where is method overriding used in real-world Python applications?
It’s used in course management, payment systems, notifications, banking operations, and user authentication for customized behavior.