Skip to content
Related Articles

Related Articles

Improve Article

Difference between Method Overloading and Method Overriding in Python

  • Difficulty Level : Easy
  • Last Updated : 29 Apr, 2021

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: 
 

Python3




# Function to take multiple arguments
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 =''
   
    # Traverse through the arguments
    for x in args:
   
        # This will do addition if the 
        # arguments are int. Or concatenation 
        # if the arguments are str
        answer = answer + x
   
    print(answer)
   
# Integer
add('int', 5, 6)
   
# String
add('str', 'Hi ', 'Geeks')

Output: 
 

11
Hi Geeks

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 of Method Overriding in python: 
 

Python3




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 is
    # 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()

Output: 
 

Modified feature_1 of class A by class B

  
Difference between Method Overloading and Method Overriding in Python: 
  
 



S.NOMethod OverloadingMethod 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.

 

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :