Method Overriding & Method Overloading in Python [with Example]

Python is a computer programming language that is used for building websites and software, automating tasks and conducting data analysis. It is one of the most popular and widely used programming languages in the world today. It has been involved in creating Netfix’s recommendation algorithm to even software for self-driving cars. 

Being a general purpose language, it is especially designed for using in a range of applications including Data Science, software and web development, automation, etc. Accordingly, in Python methods are crucial when it comes to object-oriented programming (OOP). 

There are two types of methods in OOP including method overloading and method overriding. Method overloading and method overriding in Python are two distinct ideas and its applictaions are effective. The following blog will discuss the methods with examples and how they work while providing you knowledge on the key differences between them. 

Method Overloading in Python:

In Python, method overloading is a concept that allows a class to have multiple methods with the same name but different parameter lists. Unlike some other programming languages like Java, Python does not directly support method overloading, where you can define multiple methods with different parameter lists. 

However, you can achieve method overloading-like behavior in Python using default arguments or variable-length arguments (*args and **kwargs).

Using default arguments, you can create a method that can handle multiple parameter lists by providing default values for some parameters. This way, the method can be called with different numbers of arguments, and the default values will be used for the missing ones.

Using variable-length arguments (*args), you can create a method that can accept any number of arguments. This is useful when you want a method to work with a variable number of arguments without explicitly defining them.

Using variable-length keyword arguments (**kwargs), you can create a method that can accept keyword arguments. This provides even more flexibility in handling different combinations of parameters.

Method overloading in Python allows you to create versatile and adaptable methods that can handle various argument scenarios. It promotes code reusability and enhances the flexibility of your classes, making your code more robust and maintainable. 

While Python does not enforce strict method overloading, you can use these flexible argument handling techniques to achieve similar functionality.

Method Overloading in Python with Example:

Method overloading is a feature in programming languages that allows a class to have multiple methods with the same name but different parameter lists. The method called will depend on the number or type of arguments provided during the function call. Here are some examples of method overloading in Python:

Example 1: Overloading methods with different parameter types

Overloading methods with different parameter types

Example 2: Overloading methods with different parameter types

Overloading methods with different parameter types 2

Note that method overloading is not directly supported in Python as it is in some other programming languages like Java. In Python, only the last defined method with the same name will be used, and the previous ones will be overridden. The above examples will not work as intended in Python.

To achieve method overloading-like behavior in Python, you can use default arguments or variable-length arguments (*args, **kwargs) to handle different parameter lists. Here’s an example using variable-length arguments:

Eg 2(b)

You should keep in mind that in Python, method overloading is not a strict requirement, as the language relies on duck typing. It allows functions to accept different types of arguments without explicit method overloading. 

However, using *args and **kwargs can give you more flexibility and achieve similar results as method overloading in other languages.

How do you overload a method in Python?

In Python, method overloading is achieved by using default arguments or variable-length arguments (*args, **kwargs) to handle different parameter lists. The idea is to define a single method with a generic name that can accept multiple combinations of arguments. 

Then the appropriate logic inside the method will handle different cases based on the arguments passed. Here’s how you can overload a method in Python:

  • Example using default arguments:

Example using default argument

  • Example using variable-length arguments:

Example using variable-length arguments

  • Example using variable-length keyword arguments:

Example using variable-length keyword arguments

By using default arguments or variable-length arguments, you can create methods in Python that can handle multiple argument combinations, effectively achieving method overloading-like behavior.

Method Overriding in Python:

Method overriding is a crucial concept in object-oriented programming (OOP). It allows a subclass to provide a specific implementation for a method already defined in its superclass. 

In Python, method overriding is achieved by defining a method in the subclass with the same name and the same number of parameters as the method in the superclass.

When an object of the subclass calls the overridden method, Python uses implementation from the subclass instead of the one in the superclass. This enables the subclass to customize the behavior of the method while preserving the same method signature as the superclass, ensuring a consistent interface across the class hierarchy.

Method overriding promotes code reuse and flexibility in OOP. It allows you to create specialized versions of methods in subclasses, tailoring their functionality to suit the specific needs of each subclass. 

This behavior is essential in building scalable and maintainable applications, as it enables you to extend the behavior of existing classes without modifying their source code. Method overriding is a powerful tool in Python, facilitating the creation of robust and modular object-oriented systems.

Method Overriding in Python with Example:

Method overriding is a concept in object-oriented programming where a subclass provides a specific implementation for a method that is already defined in its superclass. This allows the subclass to customize the behavior of the method while maintaining the same method signature. Here’s an example of method overriding in Python:

Method Overriding in Python with Example

In this example, we have a superclass Animal with a method make_sound(), which returns a generic sound. The subclasses Dog and Cat extend the Animal class and override the make_sound() method with their specific implementations. 

When we call make_sound() on instances of the subclasses, Python will use the overridden methods defined in the subclass instead of the one in the superclass.

It’s essential to note that method overriding is possible when there is an inheritance relationship between classes (i.e., the subclass extends the superclass). If there is no inheritance, method overriding does not occur, and Python will call the method defined in the class where the function overriding in Python call is made.

Here’s an example to demonstrate this:

In this example, class C extends both classes A and B. When we call foo() on an instance of C, Python will use the foo() method from class A (the first class in the list of base classes). If class C only extended class B, the output would have been “Class B”.

Method overriding is a powerful mechanism for customizing and extending the behavior of classes in object-oriented programming. It allows you to provide specialized implementations in subclasses while maintaining a consistent interface through the superclass.

How to do Method Overriding in Python?

Method overriding is a concept in object-oriented programming (OOP) where a subclass provides a specific implementation for a method that is already defined in its superclass. When an object of the subclass calls the overridden method, Python will use the implementation from the subclass instead of the one in the superclass.

Method overriding is based on inheritance, which is one of the key features of OOP. Inheritance allows a class (subclass) to inherit properties and behaviors from another class (superclass). The subclass can then extend or modify the functionality of the superclass to create a more specialized version of the class.

Here’s a step-by-step explanation of how to override a method in python:

  • Superclass and Subclass Relationship:

      1. In Python, we define classes using the class keyword. The class that is being inherited is called the superclass (or parent class), and the class that inherits from the superclass is called the subclass (or child class).
      2. To create a subclass, include the name of the superclass inside parentheses after the subclass name, like this: class SubclassName(SuperclassName):.
  • Method Definition in Superclass:

      1. The superclass contains a method that you want to override in the subclass.
      2. The method in the superclass must have the same name as the method you want to override in the subclass. This is essential for method overriding to work.
      3. The method in the superclass can have any implementation that you want to share among its subclasses.
  • Method Definition in Subclass:

      1. In the subclass, define a method with the same name and the same number of parameters as the method you want to override in the superclass.
      2. The method definition in the subclass will replace the implementation of the method in the superclass. This means when you call the method on an instance of the subclass, Python will use the overridden method from the subclass, not the one from the superclass.
  • Calling the Overridden Method:

      1. When you create an instance of the subclass and call the method, Python first looks for the method in the subclass.
      2. If Python finds the method in the subclass, it executes the overridden method from the subclass.
      3. If Python does not find the method in the subclass, it looks for the method in the superclass and executes it.

Here’s the example of method overriding:

Here's the example of method overriding

In this example, Animal is the superclass, and Dog and Cat are subclasses. Both Dog and Cat override the make_sound() method defined in the Animal class with their specific implementations.

Method overriding allows you to create more specialized and flexible classes in your code. It promotes code reuse, as you can define common behaviors in the superclass and customize them in the subclasses. This is one of the powerful tools in OOP that enables you to build complex and scalable applications in Python.

Difference Between Method Overloading and Method Overriding in Python:

The table below demonstrates Method Overloading vs Method Overriding in Python. Accordingly, the differences between Python Overloading and Overriding are as follows: 

Method Overloading 

Method Overriding 

It refers to defining of multiple methods with same name but parameters are different.  It defines the method in a subclass which has the same name as one of its superclass. 
Use of default arguments can help in achieving method overloading.  It can be achieved by defining the method in a subclass with the same name as one of its superclass. 
Based on the input parameters, it allows a class to have multiple methods with same name but different behaviours.  It allows a subclass in providing its own implementation of a method defined in the superclass. 
The method choice is determined during the compile time based on number and types of arguments passed to the method.  Choice of calling a method is determined at runtime based on the actual object. 
The method is not natively supported in Python.  Supported natively in Python 

Conclusion

In conclusion, understanding of the differences between method overloading and method overriding in Python is essential in the world of OOP. While the ideas are easily confused, you need to have a clear understanding of these concepts before you apply them in companies. 

If you want to learn the methods in Python programming, you can significantly opt for Data Science Courses online or Data Science Bootcamps. Pickl.AI ensures that you develop practical skills through its Python for Data Science and Data Science Job Guarantee Program. 

Moreover, you can go through Python Interview Questions and Answers to ace your interview with complete preparation on the type of questions you need to have a clear understanding of.