Discover the Secrets of Method Overriding in Python!

By Rohit Sharma

Updated on Oct 09, 2025 | 22 min read | 44.86K+ views

Share:

Python, developed by Guido van Rossum in 1989, quickly became one of the most widely adopted programming languages due to its simplicity and versatility. Among its many powerful features, method overriding stands out as a crucial concept that allows you to modify the behavior of inherited methods, giving you the flexibility to customize code to your exact requirements.

Method overriding in Python allows a child class to redefine a method that already exists in its parent class. It’s a key part of object-oriented programming that helps developers customize behavior, promote reusability, and make code more flexible. By using method overriding, you can extend or modify inherited methods without changing the original class structure. 

In this guide, you’ll read more about what method overriding in Python means, how it works, and the rules you must follow. You’ll also explore the difference between overriding and overloading, practical examples, use of the super() function, and real-world applications that every Python developer should understand. 

Sharpen your Python skills by learning method overriding and applying it in real code. Enroll in upGrad’s Online Software Engineering Course, featuring an updated curriculum on generative AI and specializations like full-stack development. 

What is Method Overriding in Python? 

At its core, method overriding in Python is a concept tied directly to inheritance. Inheritance is the OOP principle where one class (the child class) inherits attributes and methods from another class (the parent class). Overriding happens when the child class is not satisfied with the implementation of a specific method from the parent and decides to create its own version. 

To put it simply: 

  • If a parent class and a child class both have a method with the same name and parameters, the child class method gets executed when called on a child object. 
  • The child’s method overrides the parent’s method. 
  • Example analogy: 
  1. A master chef (parent class) has a generic recipe for make_sauce(). 
  2. A junior chef (child class) specializing in Italian food inherits the recipe. 
  3. The junior chef overrides make_sauce() to create a version specific to pasta. 
  • The method name remains the same, but the implementation is specialized. 

The primary purpose of this is to achieve polymorphism, which means "many forms." It allows objects of different classes to be treated as objects of a common parent class, but still execute their own specific behaviors. 

Also Read: Polymorphism in OOP: What is It, Its Types, Examples, Benefits, & More 

The Building Blocks of Overriding 

To fully grasp the concept, you must be familiar with these terms: 

  • Parent Class (Superclass or Base Class): The class whose properties are inherited by another class. 
  • Child Class (Subclass or Derived Class): The class that inherits properties from another class. It's in the child class that method overriding in Python takes place. 
  • Inheritance: The mechanism by which one class acquires the properties of another. 

Let’s look at a very basic code example to see this in action. 

Python 
# Parent Class 
class Animal: 
    def speak(self): 
        return "Some generic animal sound" 
 
# Child Class 
class Dog(Animal): 
    # Overriding the speak method 
    def speak(self): 
        return "Woof! Woof!" 
 
# Create instances of the classes 
generic_animal = Animal() 
my_dog = Dog() 
 
# Call the speak method on both objects 
print(f"Animal says: {generic_animal.speak()}") 
print(f"Dog says: {my_dog.speak()}") 
 

Output: 

Animal says: Some generic animal sound 
Dog says: Woof! Woof! 
 

In this example, the Dog class inherits from the Animal class. Both classes have a speak() method. When we call speak() on the my_dog object (an instance of Dog), it executes the version of the method defined within the Dog class, not the Animal class. This is a perfect illustration of method overriding in Python. 

 

How to Implement Method Overriding in Python: A Step-by-Step Guide 

Implementing method overriding in Python is a straightforward process that follows a logical sequence. By following these steps, you can create specialized behavior in your subclasses while maintaining a common interface defined by the superclass. 

Step 1: Define a Parent Class with a Method 

First, you need a base or parent class. This class should contain the method that you intend to override in the child class. This method provides a default implementation. 

Python 
class Employee: 
    def __init__(self, name, salary): 
        self.name = name 
        self.salary = salary 
 
    def get_role(self): 
        return "A general employee in the company." 
 

Here, the Employee class has a get_role() method that returns a generic description. 

Step 2: Create a Child Class that Inherits from the Parent 

Next, define the child class. The syntax for inheritance in Python involves placing the parent class's name in parentheses after the child class's name. 

Python 
class Manager(Employee): 
    # The Manager class will go here 
    pass 
 

The Manager class now inherits all attributes and methods from the Employee class, including get_role(). 

Step 3: Redefine the Method in the Child Class 

This is the core step of method overriding in Python. In the child class, you define a method with the exact same name and signature (number and type of parameters) as the method in the parent class. 

Python 
# Parent Class 
class Employee: 
    def __init__(self, name, salary): 
        self.name = name 
        self.salary = salary 
 
    def get_role(self): 
        return "A general employee in the company." 
 
# Child Class 
class Manager(Employee): 
    # Overriding the get_role method 
    def get_role(self): 
        return "A manager responsible for a team." 
 

Data Science Courses to upskill

Explore Data Science Courses for Career Progression

background

Liverpool John Moores University

MS in Data Science

Double Credentials

Master's Degree17 Months

Placement Assistance

Certification6 Months

Step 4: Create Instances and Observe the Behavior 

Now, let's create objects of both the Employee and Manager classes and call the get_role() method on each. This demonstrates how the overridden method is chosen at runtime. 

Python 
# Create instances 
regular_employee = Employee("John Doe", 60000) 
project_manager = Manager("Jane Smith", 90000) 
 
# Call the get_role method 
print(f"{regular_employee.name}'s role: {regular_employee.get_role()}") 
print(f"{project_manager.name}'s role: {project_manager.get_role()}") 
 
 

Output: 

John Doe's role: A general employee in the company. 
Jane Smith's role: A manager responsible for a team. 
 

As you can see, Python’s Method Resolution Order (MRO) first looks for the get_role() method within the Manager class. Since it finds it there, it executes it. For the regular_employee object, it looks in the Employee class and executes that version. This dynamic selection is the essence of method overriding in Python. 

Also Read: Class in Python 

Using the super() Function with Method Overriding 

Sometimes, you don't want to completely replace the parent class's method. Instead, you might want to add to it or extend its functionality. This is where the super() function becomes incredibly useful in the context of method overriding in Python. 

The super() function gives you access to methods and properties of a parent or sibling class. When used inside an overridden method, it allows you to call the parent class's version of that same method, letting you reuse its code before or after adding new functionality. 

Let's expand on our Employee and Manager example. A manager is still an employee. So, when we ask for their details, we might want to display the standard employee details first and then add the manager-specific information. 

Python 
# Parent Class 
class Employee: 
    def __init__(self, name, employee_id): 
        self.name = name 
        self.employee_id = employee_id 
 
    def display_details(self): 
        print(f"Name: {self.name}") 
        print(f"Employee ID: {self.employee_id}") 
 
# Child Class 
class Manager(Employee): 
    def __init__(self, name, employee_id, team_size): 
        # Call the parent constructor 
        super().__init__(name, employee_id) 
        self.team_size = team_size 
 
    # Overriding the display_details method 
    def display_details(self): 
        print("--- Manager Details ---") 
        # Call the parent class's display_details method 
        super().display_details() 
        # Add new functionality 
        print(f"Team Size: {self.team_size}") 
 
# Create an instance of Manager 
mgr = Manager("Alice Johnson", "MGR101", 12) 
mgr.display_details() 
 

Output: 

--- Manager Details --- 
Name: Alice Johnson 
Employee ID: MGR101 
Team Size: 12 
 

In this improved example: 

  1. The Manager's __init__ method uses super().__init__(...) to call the Employee constructor, avoiding code duplication for initializing name and employee_id. 
  2. The overridden display_details method in Manager first calls super().display_details() to print the basic employee information. 
  3. It then adds its own specific logic by printing the team_size. 

This approach is highly efficient and aligns with the Don't Repeat Yourself (DRY) principle of software development. 

Also Read: Top 50 Python Project Ideas with Source Code in 2025 

Complete Override vs. Extending with super() 

The following table summarizes the difference between a complete override and extending a method. 

Feature  Complete Override  Extending with super() 
Purpose  To provide a completely new implementation.  To add to the existing implementation from the parent. 
Parent Method  The parent class's method is ignored.  The parent class's method is called and executed. 
Code Reusability  Low. You rewrite the logic from scratch.  High. You reuse the parent's logic. 
Use Case  When the child's behavior is entirely different.  When the child's behavior is a specialized version of the parent's. 

Using super() is a key technique when working with method overriding in Python for building robust, layered class hierarchies. 

Also Read: Top 36+ Python Projects for Beginners and Students to Explore in 2025 

Method Overriding vs. Method Overloading: Key Differences 

A common point of confusion for programmers, especially those coming from languages like Java or C++, is the distinction between method overriding and method overloading. While they sound similar, they are fundamentally different concepts. 

Method Overloading refers to defining multiple methods in the same class with the same name but with a different number or type of parameters. The correct method is chosen based on the arguments passed during the call. Python does not support traditional method overloading by default. You can simulate it using default arguments or variable-length argument lists (*args, **kwargs), but it's not a built-in feature based on parameter types. 

Method Overriding, as we've discussed, involves a method in a child class that has the same name and signature as a method in its parent class

Here is a table that breaks down the critical differences: 

Basis of Comparison  Method Overriding  Method Overloading 
Definition  A child class redefines a method of its parent class.  A class has multiple methods with the same name but different parameters. 
Class Relationship  Occurs between two classes (parent and child) linked by inheritance.  Occurs within the same class. 
Method Signature  The method name, parameters, and return type must be the same.  The method name is the same, but the number or type of parameters is different. 
Purpose  To provide a specific implementation for an inherited method (achieves polymorphism).  To provide different ways to perform a similar operation (increases readability). 
Python Support  Fully supported and a core feature of Python's OOP.  Not directly supported. Can be simulated using optional parameters or *args. 

Understanding this distinction is crucial for writing clear and correct object-oriented code. Method overriding in Python is about specializing behavior across a class hierarchy, while overloading is about providing flexible method calls within a single class. 

Also Read: A Complete Guide To Method Overloading in Python (With examples) 

Real-World Examples of Method Overriding in Python 

Theoretical explanations are helpful, but seeing method overriding in Python in practical scenarios solidifies the concept. Here are a few real-world examples. 

Example 1: Geometric Shapes 

Imagine a graphics application where you need to calculate the area of different shapes. You can have a base Shape class with a default calculate_area method, and specific shape classes can override it. 

Python 
import math 
 
# Parent class 
class Shape: 
    def calculate_area(self): 
        return "Area calculation is not defined for a generic shape." 
 
# Child class 1 
class Circle(Shape): 
    def __init__(self, radius): 
        self.radius = radius 
 
    # Overriding the method 
    def calculate_area(self): 
        return math.pi * (self.radius ** 2) 
 
# Child class 2 
class Rectangle(Shape): 
    def __init__(self, width, height): 
        self.width = width 
        self.height = height 
 
    # Overriding the method 
    def calculate_area(self): 
        return self.width * self.height 
 
# Using the classes 
my_circle = Circle(10) 
my_rectangle = Rectangle(5, 4) 
 
print(f"Area of Circle: {my_circle.calculate_area():.2f}") 
print(f"Area of Rectangle: {my_rectangle.calculate_area()}") 
 

Output: 

Area of Circle: 314.16 
Area of Rectangle: 20 
 

Here, Circle and Rectangle both provide their own specific formula for calculate_area, overriding the placeholder method in the Shape class. This is a classic example of method overriding in Python that enables polymorphic behavior. 

Example 2: Document Rendering 

Consider a system that renders documents in different formats like PDF or HTML. A base Document class can have a render method, which is then overridden by subclasses. 

Python 
# Parent class 
class Document: 
    def __init__(self, content): 
        self.content = content 
 
    def render(self): 
        raise NotImplementedError("Subclass must implement this method") 
 
# Child class 1 
class PdfDocument(Document): 
    def render(self): 
        print(f"Rendering content as a PDF: '{self.content}'") 
 
# Child class 2 
class HtmlDocument(Document): 
    def render(self): 
        print(f"Rendering content as HTML: <p>{self.content}</p>") 
 
# A function that works with any Document type 
def render_document(doc): 
    print("--- Starting document render ---") 
    doc.render() 
    print("--- Render complete ---") 
 
# Create and render documents 
pdf = PdfDocument("This is my CV.") 
html = HtmlDocument("This is a blog post.") 
 
render_document(pdf) 
render_document(html) 
 

Output: 

--- Starting document render --- 
Rendering content as a PDF: 'This is my CV.' 
--- Render complete --- 
--- Starting document render --- 
Rendering content as HTML: <p>This is a blog post.</p> 
--- Render complete --- 
 

The render_document function can work with any object that is a Document subtype, without needing to know its specific type. It simply calls the render() method, and polymorphism ensures the correct version is executed. 

Enhance Your Python Skills with upGrad! 

Method overriding in Python is more than just a language feature; it is a cornerstone of effective object-oriented design. It empowers developers to build flexible and maintainable systems by allowing child classes to customize or extend the behavior inherited from parent classes. By defining methods in a subclass with the same signature as those in the superclass, you leverage the power of polymorphism, making your code cleaner and more intuitive. 

As you strengthen your understanding of OOP concepts like method overriding, applying them in structured, project-based settings is essential. That’s where upGrad comes in, with hands-on courses and expert guidance, it helps you build job-ready expertise. 

Curious about which Python software development course best fits your goals in 2025? Contact upGrad for personalized counseling and valuable insights, or visit your nearest upGrad offline center for more details. 

Unlock the power of data with our popular Data Science courses, designed to make you proficient in analytics, machine learning, and big data!

Subscribe to upGrad's Newsletter

Join thousands of learners who receive useful tips

Promise we won't spam!

Elevate your career by learning essential Data Science skills such as statistical modeling, big data processing, predictive analytics, and SQL!

Stay informed and inspired with our popular Data Science articles, offering expert insights, trends, and practical tips for aspiring data professionals!

Frequently Asked Questions (FAQs)

1. What happens if the method signatures don't match exactly during overriding?

If the method name is the same but the parameters are different, it is not considered overriding. Instead, the child class will simply have a new method, and the parent's method will still be inherited. This can lead to unexpected behavior if you intended to override. 

2. Can I override a constructor (__init__) in Python?

Yes, you absolutely can. Overriding the __init__ method is very common. It's often done to add child-specific attributes, and you typically use super().__init__(...) inside it to call the parent's constructor and initialize inherited attributes. 

3. Is it possible to prevent a method from being overridden in Python?

Unlike languages like Java (with the final keyword), Python does not have a built-in keyword to prevent method overriding. The convention is to use a double underscore prefix (e.g., __my_method) to perform name mangling, making it much harder, but not impossible, to override accidentally. 

4. Can private methods (those with a double underscore prefix) be overridden?

Due to name mangling, methods like __private_method in a class MyClass are renamed to _MyClass__private_method. A child class defining its own __private_method will have it mangled differently, so it doesn't technically override the parent's method but creates a new, separate one. 

5. What is the role of the @override decorator?

Starting with Python 3.12, the typing module includes an @override decorator. Its purpose is to declare intent. If you decorate a method with @override and it doesn't actually override a method from a parent class, a static type checker will flag it as an error, helping you catch mistakes early. 

6. Can static methods or class methods be overridden?

Yes, both static methods and class methods can be overridden in a subclass. The same rules apply: the method in the subclass will be called, and you can use super() to access the parent's version if needed. 

7. How does multiple inheritance affect method overriding?

With multiple inheritance, Python uses the Method Resolution Order (MRO) to determine which method to call. It follows a specific search path through the parent classes (from left to right in the class definition). The first method it finds with the matching name is the one that gets executed. 

8. Is method overriding a form of polymorphism?

Yes, it is the primary way to achieve runtime polymorphism in Python. It allows objects of different classes to respond to the same method call in different, class-specific ways. 

9. Can I call an overridden method from the parent class directly?

Yes, besides using super(), you can call the parent method explicitly by specifying the parent class name, like ParentClassName.method_name(self, args). However, using super() is generally preferred as it is more maintainable and works better with multiple inheritance. 

10. Is it mandatory to override a method in a child class?

No, it is not mandatory. If you don't provide an implementation for a method in the child class, it will automatically inherit and use the version from the parent class when called on a child class object. 

11. What is an abstract method and how does it relate to overriding?

An abstract method is a method declared in a parent class (often an Abstract Base Class or ABC) that has no implementation. Subclasses are required to provide an implementation by overriding it. If a subclass fails to override the abstract method, you cannot create an instance of that subclass. 

12. Does the return type matter in method overriding in Python?

Python is a dynamically typed language, so it does not enforce the return type to be the same. However, for code clarity and predictability, it is a very good practice to have the overridden method return a value of the same or a compatible type. 

13. What's the difference between overriding and shadowing a method?

In the context of Python, these terms are often used interchangeably. When a child class defines a method with the same name as a parent's method, the child's method "shadows" or "overrides" the parent's version for instances of the child class. 

14. Can operators like __add__ or __str__ be overridden?

Yes, overriding these special "dunder" (double underscore) methods is extremely common. For example, overriding __str__ allows you to define a custom string representation for your objects when they are passed to print() or str(). 

15. Does overriding affect the performance of my code?

The performance impact of method overriding is negligible in almost all applications. Python's method dispatch mechanism is highly optimized. You should focus on writing clear, maintainable, and correct code rather than worrying about a performance hit from overriding. 

16. What if a child class inherits from two parents that have methods with the same name?

This is the "diamond problem" in multiple inheritance. Python's MRO resolves this predictably. It linearizes the inheritance graph, and when you call the method, it executes the version from the first class in the MRO that contains it. 

17. How can I check an object's Method Resolution Order (MRO)?

You can check the MRO of a class by accessing its __mro__ attribute or by calling the .mro() method on the class. For example, Manager.__mro__ or Manager.mro() will show the order in which Python will search for methods. 

18. Is there a limit to how many times a method can be overridden in an inheritance chain?

No, there is no limit. A method can be defined in a base class, overridden in a child class, and then overridden again in a grandchild class, and so on. Each class in the chain can provide its own specific implementation. 

19. What is the benefit of using super() over the hardcoded parent class name?

Using super() decouples the child class from the parent class's name. If you later change the parent class, you only need to change it in the class definition line, not every place you call a parent method. It also makes the code work correctly with multiple inheritance. 

20. When should I avoid method overriding?

You should avoid overriding if the parent class's method implementation is exactly what you need. Overriding without adding or changing functionality just adds unnecessary code. Also, be cautious when overriding core methods of built-in types unless you have a very clear reason to do so. 

Rohit Sharma

840 articles published

Rohit Sharma is the Head of Revenue & Programs (International), with over 8 years of experience in business analytics, EdTech, and program management. He holds an M.Tech from IIT Delhi and specializes...

Speak with Data Science Expert

+91

By submitting, I accept the T&C and
Privacy Policy

Start Your Career in Data Science Today

Top Resources

Recommended Programs

IIIT Bangalore logo
bestseller

The International Institute of Information Technology, Bangalore

Executive Diploma in DS & AI

360° Career Support

Executive PG Program

12 Months

Liverpool John Moores University Logo
bestseller

Liverpool John Moores University

MS in Data Science

Double Credentials

Master's Degree

17 Months

upGrad Logo

Certification

3 Months