Method Overloading
Method overloading or function overloading is a type of polymorphism in which we can define a number of methods with the same name but with a different number of parameter as well as parameters can be different types. These methods can perform a different task.
When we have multiple methods with the same name but different arguments or types of arguments in a single class then we call it "method overloading".
"Python does not support method overloading on the basis of different numbers of parameters in functions."
We can overload the methods but can only use the latest defined method.
Example
# first product method takes two arguments
def product(a,b):
p=a*b
print(p)
# second product method takes three arguments
def product(a,b,c):
p=a*b*c
print(p)
product(4,5) # This line shows an error
product(2,5,4) # This statement will call the second product method
In the above code we have defined two product method, but we can only use the second product method. As python does not support method overloading. We may define many method of the same name and different arguments but we can only use the latest defined method. Calling the other method will produce an error. Like here calling product(4,5) will produce an error as the latest defined method takes three arguments.
So we can only call the latest product method which takes three arguments.
No comments:
Post a Comment