The Wayback Machine - https://web.archive.org/web/20240701101628/https://www.geeksforgeeks.org/difference-between-method-overloading-and-method-overriding-in-python/
Open In App

Difference between Method Overloading and Method Overriding in Python

Last Updated : 29 Apr, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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.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
The differences between Method Overloading and Method Overriding in Java are as follows: Method Overloading Method Overriding Method overloading is a compile-time polymorphism.Method overriding is a run-time polymorphism.Method overloading helps to increase the readability of the program.Method overriding is used to grant the specific implementatio
4 min read
Function Overloading vs Function Overriding in C++
Function Overloading (achieved at compile time) Function Overloading provides multiple definitions of the function by changing signature i.e. changing number of parameters, change datatype of parameters, return type doesn’t play any role. It can be done in base as well as derived class.Example: void area(int a); void area(int a, int b); C/C++ Code
3 min read
Method Overriding in Python
Prerequisite: Inheritance in Python Method overriding is an ability of any object-oriented programming language that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes. When a method in a subclass has the same name, same parameters or signature and
5 min read
Overriding Nested Class members in Python
Overriding is an OOP's (object-oriented programming) concept and generally we deal with this concept in Inheritance. Method overriding is an ability of any object-oriented programming language that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes
2 min read
Overriding toString() method in Scala
Consider the following Scala program: // Scala Program without override toString method // Creating Class class Language(LangArticle: Int, LangName: String) { // Defining getLangArticle method def getLangArticle() : Int = { return LangArticle; } // Defining getLangName method def getLangName() : String = { return LangName; } } // Creating object ob
2 min read
Ruby | Method overriding
Method is a collection of statements that perform some specific task and return the result. Override means two methods having same name but doing different tasks. It means that one of the methods overrides another method. If there is any method in the superclass and a method with the same name in its subclass, then by executing these methods, metho
2 min read
Overriding the save method - Django Models
The save method is an inherited method from models.Model which is executed to save an instance into a particular Model. Whenever one tries to create an instance of a model either from admin interface or django shell, save() function is run. We can override save function before storing the data in the database to apply some constraint or fill some r
3 min read
Difference between Multi-methods and Overloading
In multi-methods we use the collection of methods. And of all methods have the same name, the same number of arguments, and can have overlapping type signatures. In the case of multi-methods when a function call is made then a collection of all the methods is considered as possible methods. Depending upon the argument at run-time, the correct metho
4 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 interface for developers. In this article, we will lea
4 min read
Python | Method Overloading
Method Overloading: Two or more methods have the same name but different numbers of parameters or different types of parameters, or both. These methods are called overloaded methods and this is called method overloading. Like other languages (for example, method overloading in C++) do, python does not support method overloading by default. But ther
4 min read
three90RightbarBannerImg