Method Overloading class Number:
class Number: def __init__(Self,a,b,c):
def __init__(Self,a,b,c): Self.a=a
Self.b=b
Self.a=a Self.c=c
Self.b=b #Predefined syntax
Self.c=c def __add__(Self,other):
N1=Number(1,2,3) a=Self.a+other.a
b=Self.b+other.b
N2=Number(4,5,6) c=Self.c+other.c
res=Number(a,b,c)
N1+N2 (5,7,9) return res
def __lt__(Self,other):
o1=self.a+self.b+self.c
o2=other.a+other.b+other.c
if(o1<o2):
return True
else:
N1=Number(1,2,3) return False
N2=Number(4,5,6)
N3=N1+N2 (5,7,9) #N1 is assigned to self, N2 is for
other
print(N3.a,N3.b,N3.c)
Method Overloading
Python doesn’t support Method Overloading we have to
use default arguments.
If we use default arguments in method declaration
class MethodOverloading:
def display(self, a=None, b=None, c=None):
print(a,b,c)
Obj=MethodOverloading()
Obj.display()
Obj.display(10)
Obj.display(10,20)
Obj.display(10,20,30)
Method Overriding
Is implemented only during Inheritance:
class Base:
def display(Self):
print(“Base Class display”)
class Derived(Base): #if it is not satisfied base class methods definition
def display(Self): #Derived class is redefining the base class method
print(“Derived Class display()”)
obj=Derived()
obj.display()