Difference between Method Overloading and Method Overriding in Python
Last Updated :
05 Apr, 2025
Method Overloading and Method Overriding are two key concepts in object-oriented programming that help you manage methods in classes. Both concepts allow you to define methods in different ways, but they serve different purposes and behave differently. Method overloading provides flexibility with function signatures, and method overriding offers a way to customize or extend the behavior of inherited methods.
Method Overloading
Method Overloading is an example of Compile time polymorphism. In this, more than one method of the same class shares the same method name having different signatures. Method overloading is used to add more to the behavior of methods and there is no need of more than one class for method overloading.
Note: Python does not support method overloading. We may overload the methods but can only use the latest defined method.
Example:
Python
def add(datatype, *args):
# if datatype is int initialize answer as 0
if datatype =='int':
answer = 0
# if datatype is str initialize answer as ''
if datatype =='str':
answer =''
for x in args:
answer = answer + x
print(answer)
# Integer
add('int', 5, 6)
# String
add('str', 'Hi ', 'Geeks')
Explanation: This code demonstrates method overloading in Python using *args. The add() function performs different operations based on the datatype argument. If datatype is 'int', it sums the integers in args. If datatype is 'str', it concatenates the strings in args. The function adapts to different input types, simulating overloading.
Method Overriding
Method overriding is an example of run time polymorphism. In this, the specific implementation of the method that is already provided by the parent class is provided by the child class. It is used to change the behavior of existing methods and there is a need for at least two classes for method overriding. In method overriding, inheritance always required as it is done between parent class(superclass) and child class(child class) methods. Example:
Python
class A:
def fun1(self):
print('feature_1 of class A')
def fun2(self):
print('feature_2 of class A')
class B(A):
# Modified function that already exist in class A
def fun1(self):
print('Modified feature_1 of class A by class B')
def fun3(self):
print('feature_3 of class B')
# Create instance
obj = B()
# Call the override function
obj.fun1()
OutputModified feature_1 of class A by class B
Explanation: This code demonstrates method overriding in Python. Class B inherits from class A and overrides the fun1() method. When fun1() is called on an instance of B, the overridden method in class B is executed instead of the original method in class A.
Difference between Method Overloading and Method Overriding in Python
S.NO | Method Overloading | Method Overriding |
---|
1. | In the method overloading, methods or functions must have the same name and different signatures. | Whereas in the method overriding, methods or functions must have the same name and same signatures. |
2. | Method overloading is a example of compile time polymorphism. | Whereas method overriding is a example of run time polymorphism. |
3. | In the method overloading, inheritance may or may not be required. | Whereas in method overriding, inheritance always required. |
4. | Method overloading is performed between methods within the class. | Whereas method overriding is done between parent class and child class methods. |
5. | It is used in order to add more to the behavior of methods. | Whereas it is used in order to change the behavior of exist methods. |
6. | In method overloading, there is no need of more than one class. | Whereas in method overriding, there is need of at least of two classes. |
Similar Reads
Difference Between Method Overloading and Method Overriding in Java Understanding the difference between Method Overloading and Method Overriding in Java plays a very important role in programming. These two are the important concepts that help us to define multiple methods with the same name but different behavior, both of these are used in different situations. Th
6 min read
Difference Between Function Overloading and Function Overriding in JavaScript Function overloading and function overriding are two important concepts in object-oriented programming (OOP). Function overloading allows the creation of multiple functions with the same name but different parameters. Function overriding allows the subclass or child class to provide the specific imp
3 min read
Difference between Method and Function in Python Here, key differences between Method and Function in Python are explained. Java is also an OOP language, but there is no concept of Function in it. But Python has both concept of Method and Function. Python Method Method is called by its name, but it is associated to an object (dependent).A method d
3 min read
Method And Constructor Overloading In Python In object-oriented programming, method, and constructor overloading are powerful features that allow developers to define multiple methods or constructors with the same name but with different parameters. This flexibility enhances code readability, and reusability, and provides a more intuitive inte
4 min read
Difference Between x = x + y and x += y in Python We often use x += y instead of x = x + y. So, are they same or different? Let's Find it here. Example 1: Python3 x = [1, 2] another_x = x y = [3] x += y print(x) print(another_x) Output: [1, 2, 3] [1, 2, 3] Example 2: Python3 x = [1, 2] another_x = x y = [3] x = x + y print(x) print(another_x) Outpu
2 min read
Difference between abstract class and interface in Python In this article, we are going to see the difference between abstract classes and interface in Python, Below are the points that are discussed in this article: What is an abstract class in Python?What is an interface in Python?Difference between abstract class and interface in PythonWhat is an Abstra
4 min read