Table of Contents
In this tutorial, we will discuss Method overloading in Python language
Method overloading is a OOP concept in Python language
Method overloading Is not supported in Python, except in some situations. In case, you overload methods then you will be able to access the last defined method.
we can define more than one methods in the same name and we can call that many ways with Zero, one, or more parameters but you can access only last defined method in python.
overloading is done by changing the data type of variable, number of variable and passing to the parameter list.
When we call that method, we can make the following changes
1. Empty parameter
2. different number of parameter
3. different type of parameter
above diagram shows how to call a method in various ways, x,y,z are different data type to pass parameter. When we call that method, we can use that data type in a different way in OOP’s languages
class SumNum:
def add(self,x,y,z):
return x+y+z
calc=SumNum()
print(calc.add(2,3,4))
print(calc.add(7,5,8))
print(calc.add('a','b','c'))
print(calc.add('b','c','d'))
print(calc.add(4.5,7.3,5.9))
When the above code is executed, The following output is displayed
9 20 abc bcd 17.7
| Call early definition of the method but it not supported |
Error massage - TypeError: add() takes exactly 3 arguments (2 given) Program 3
class SumNum:
def add(self,x,y):
return x+y
def add(self,x,y,z):
return x+y+z
calc=SumNum()
print(calc.add(4,7,5))
#print(calc.add(7,5))
print(calc.add('a','b','c'))
print(calc.add(4.5,7.3,5.9)) When the following code is executed, The following output is displayed
16 abc 17.7
In the above program, python allows access to the last definition method with three arguments. Second statements have two arguments but it is the comment here.
Program 4
class SumNum:
def add(self,x,y):
return x+y
def add(self,x,y,z):
return x+y+z
calc=SumNum()
print(calc.add(4,7,5))
print(calc.add(7,5))
print(calc.add('a','b','c'))
print(calc.add(4.5,7.3,5.9))
When the following code is executed, The following output is displayed
16 Traceback (most recent call last): File "C:/Python27/calcadd.py", line 11, in <module> print(calc.add(7,5)) TypeError: add() takes exactly 4 arguments (3 given)
Let’s see this simple example of the method over loading
In this example, we have two methods which have the same names but the different number of the argument list. If you know java or c++(Oop languages) we will find nothing wrong this scope. But python only understand latest implements in the add method
print(maths.add(4,7,5)) //if we call add method with three arguments python runs successful
Generate output -16
print(maths.add(7,5)) //if we call add method with two arguments python is not understand.So generate error massage
Program 4
When the following code is executed, The following output is displayed
Hello hello jhon
Multiply two numbers in Java using scanner| 5 different ways In this article, we will…
5 Different ways to Divide two numbers in Java using scanner In this article, we…
Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…
10 ways to subtract two numbers in Java In this article, we will discuss the…
Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…
How to Divide two numbers in Java| 5 different ways In this article, we will…
This website uses cookies.