Method overloading
Method overloading means method name is same and signature is different in a single class is called method overloading. Signature stands for number of arguments, types of arguments and sequence of arguments. In Method overloading, there are more than one method with the same name in single class. Method overloading is supported by the object oriented programming languages like C++ and Java.
Python does not support method overloading.
add(int a,int b,int c)
add(int a,int b)
is called method overloading. This is not supported in python but you can call same method with different number of parameters. Following example describes it.
Example:
class methodoverdemo:
def add(self,a=None,b=None,c=None):
if(a!=None and b!=None and c!=None):
print("Addition is",a+b+c)
elif(a!=None and b!=None):
print("Addition is",a+b)
else:
print("Enter two or three arguments")
m1=methodoverdemo()
m1.add(5,10,15)
m2=methodoverdemo()
m2.add(20,25)
m3=methodoverdemo()
m3.add(12.5,4.4)
Output:
Addition is 30
Addition is 45
Addition is 16.9
Method overriding
The sub class have same method as it is in super class then it is called method overriding, generally method ie overridden by subclass with some extra features then super class.
Example:
class class1:
def show(self):
print("Method of class1")
class class2(class1):
def show(self):
print("Method of class 2")
obj1=class1()
obj2=class2()
obj1.show()
obj2.show()
Output:
Method of class1
Method of class 2