Open In App

Difference between Multi-methods and Overloading

Improve
Improve
Like Article
Like
Save
Share
Report

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 method is chosen. We can say the generalization of method polymorphism is multi-methods.

In multi-methods, we generally focus on two things:

  • name of the method.
  • Dispatch value, which is produced by the dispatch method.

The correct method is chosen based on dispatch value.

Example of multi-methods:

Python3




from multimethod import multimethod
class myClass(object):
      
    @multimethod
    def GFG(self, x: int):
        print(2 * x)
  
    @multimethod
    def GFG(self, x: complex):
        print("GeeksforGeeks")
          
obj = myClass()
obj.GFG(3)
obj.GFG(6j)


Output:

6

GeeksforGeeks

In case of overloading, we have two or more methods having the same name but different signatures. Signature means argument type and the number of arguments.

It is an OOP concept, in which we have two or more functions with the same name but different parameters. In the case of function overloading name would be the same for all the functions but they will be different from each other in terms of arguments.

In function overloading, it is important that all the functions with the same name need to be different in terms of arguments they may or may not be in terms of the return type. They may have the same return type or maybe a different return type.

int test() { }

int test(int a) { }

float test(double a) { }

int test(int a, double b) { }

Here all are different in terms of arguments they may have the same return type.

int test(int a) { }

double test(int b){ }

The above code is incorrect because they are different in terms of return type but they have the same arguments. 

It is solved statically i.e, during compile time, depending upon the type of arguments and number of arguments. It is a special case of multi-methods.

Example of function overloading:

C++




#include <bits/stdc++.h>
using namespace std;
  
void display(int i) 
{
    cout << " It is int " << i << endl;
}
void display(double f) 
{
    cout << " It is float " << f << endl;
}
void display(char const *c) 
{
    cout << " It is char* " << c << endl;
}
  
int main() 
{
    display(1);
    display(1.1);
    display("GFG");
    return 0;
}


Output:

It is int 1

It is float 1.1

It is char* GFG

Following is a table of Differences between Multi-methods and Overloading

S. No.

Multi-methods

Overloading

1. In multi-methods, the method is a dispatch based on dispatch value at runtime In Overloading, the method is selected based on arguments type and number of arguments
2. Here we can have the same name and same number of arguments Here we have the same number but the different number of arguments
3. In the case of multi-methods, we have an overlapping signature type signature In case of overloading, we don’t have overlapping type signatures.
4. It is decided at runtime time It decided at compile time
5. In multi-methods we use dispatch value to dispatch method at runtime. In overloading, we use the type of argument and the number of arguments to choose the correct method at compile time.
6. It is a collection of methods that have the same name and same number of arguments In overloading, they are different in terms of arguments.
7. It belongs to the generic function It doesn’t belong to the generic function
8. It is kind of virtual on any parameter It is  not virtual
9. It depends on the actual type of receiver and arguments It depends on the declared type of the parameters


Last Updated : 11 Mar, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads