Difference between Method Overloading and Method Overriding in Python

In this article, let us look at Method Overloading and Method Overriding in python and what are the differences between Method Overloading and Method Overriding.

Method Overloading:

Method Overloading is a form of Compile time polymorphism. In the case of method overloading, multiple methods belonging to the same class can have the same method name but different signatures.

Method overloading can be used to add more to the behavior of the concerned methods. A user will not require more than one class to implement it.

NOTE:

Note that Python does not support method overloading. A user may overload 
all of the methods, but they will only be able to use the
lastest defined method.

Example

Approach:

  • Create a class say MethodOverloading
  • Create a function say multiply which accepts two numbers as arguments and prints the multiplication result of those two numbers.
  • Create another function with the same name say multiply which accepts three numbers as arguments and prints the multiplication result of those three numbers.
  • Create an object for the above MethodOverloading() class
  • Pass two numbers as arguments to the multiply() function and apply it on the above object and print the result.
  • Pass three numbers as arguments to the multiply() function and apply it on the above object and print the result.
  • The Exit of the Program.

Below is the implementation:

# Create a class say MethodOverloading
class MethodOverloading:
   # Create a function say multiply which accepts two numbers as arguments 
   # and prints the multiplication result of those two numbers
   def multiply(self, x, y):
       print(x*y)
   # Create another function with the same name say multiply which accepts three 
   # numbers as arguments and prints the multiplication result of those three numbers
   def multiply(self, x, y, z):
       print(x*y*z)

# Create an object for the above MethodOverloading() class      
obj =MethodOverloading()
# Pass two numbers as arguments to the multiply() function and apply it 
# on the above object and print the result
print(obj.multiply(2, 3))
# Pass three numbers as arguments to the multiply() function and apply it 
# on the above object and print the result
print(obj.multiply(2, 3, 4))

Output:

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-6-7d5007590dae> in <module>
14 # Pass two numbers as arguments to the multiply() function and apply it
15 # on the above object and print the result
---> 16 print(obj.multiply(2, 3))
17 # Pass three numbers as arguments to the multiply() function and apply it
18 # on the above object and print the result

TypeError: multiply() missing 1 required positional argument: 'z'

Explanation:

Python does not support method overloading because Python always uses the last defined method. We can still overload methods in Python, but it’s pointless. However, method overloading can be implemented in Java, C++, and other languages in the manner described above.

Another way to perform method overloading in Python is as follows:

# Create a class say MethodOverloading
class MethodOverloading:
    # Create a function say multiply which accepts two numbers as arguments 
    # and prints the multiplication result of those two numbers based on conditions
    def multiply(self, a = None, b = None):
        # Check if both the numbers are not equal to None using the if conditional 
        # statement, and operator
        if a!= None and b!= None:
            # If it is true then return the multiplication of both the numbers
            return  a*b
        # else if Check only the first number passed is not None using the elif conditional statement
        elif a!= None:
            # If it is true then return only the first number
            return a
        else:
            # Else return 0
            return 0

# Create an object for the above MethodOverloading() class  
class_object = MethodOverloading()

# Call the above multiply() function without passing any arguments and print the result
print("Result = ", class_object.multiply())
# Call the above multiply() function by passing only one number as an argument and print the result
print("Result = ", class_object.multiply(3))
# Call the above multiply() function by passing two numbers as arguments and print the result
print("Result = ", class_object.multiply(4, 5))

Output:

Result = 0
Result = 3
Result = 20

Method Overriding

Method overriding is a form of Run time polymorphism. The child class provides the specific implementation of the method that is already provided by the parent class. It is used to change the behavior of existing methods, and method overriding requires at least two classes. In method overriding, inheritance is always required because it occurs between methods from the parent class (superclass) and the child class (child class).

Approach:

  • Create a parent class say FirstClass
  • Inside the parent class, create a function say display1().
  • Create another function say display2.
  • Create a child class say SecondClass() which inherits properties from the parent class, FirstClass().
  • Create a function with the same name say display1 as in the parent which modifies or overrides the text when it is called.
  • Create an object for the child class say SecondClass() and store it in a variable.
  • Call the display1() function of the SecondClass() which overrides the display1() function of the FirstClass.
  • The Exit of the Program.

Below is the implementation:

# Create a parent class say FirstClass
class FirstClass:
    
    # Create a function say display1
    def display1(self):
        print('display_1 of the FirstClass')
    
    # Create another function say display2
    def display2(self):
        print('display_2 of the FirstClass')
    
# Create a child class say SecondClass which inherits properties from parent class, FirstClass
class SecondClass(FirstClass):
    
    # Create a function with the same name say display1 as in the parent parent
    # which modifies or overrides the text when it is called
    def display1(self):
        print('Overriding display1() class of FirstClass by the SecondClass')
        
    def display3(self):
        print('display_3 of the SecondClass')
        

# Create an object for the child class say SecondClass() and store it in a variable
secondClass_object = SecondClass()
    
# Call the display1() fuction of the SecondClass() which overrides the 
# display1() function of the FirstClass
secondClass_object.display1()

Output:

Overriding display1() class of FirstClass by the SecondClass

Difference between Method Overloading and Method Overriding in Python

            Method Overloading               Method Overriding
Same method name but the different number of arguments Same method name and the same number of arguments
Inheritance is not required(optional). Inheritance is not required(optional).
It is an example of compile time polymorphism. It is an example of run-time polymorphism.
It is performed between methods within the class. Method overriding occurs between parent and child class methods.
Overloading can be done within a class A minimum of two classes are required for overriding  
Overloading is used to add more to the behavior of methods. Overriding is used to change the behavior of existing methods.
Static methods can be overloaded here Static methods cannot be overloaded here
It is related with Polymorphism It is related with Inheritance.