POLYMORPHISM: ============= Poly ==> Many Morphs ==> Forms Polymorphism ==> Many forms one thing in many forms is called polymorphism. ex: digital calculator addition: two numbers ==> 1 + 2 ==> 3 three numbers ==> 10 + 20 + 30 ==> 60 four numbers + ==> addition + ==> concatenation * ==> multiplication * ==> repetition ==> Polymorphism can perform in different ways: 1) Function with class object 2) Overloading 3) Overriding 1) Function with class object ============================== # Polymorphism # method-1: Function with class object class Class1: def m1(self): print("Hello") print("Good Morning.") class Class2: def m1(self): print("Hi") print("Have a great day.") def fun(obj): obj.m1(); # obj1 = Class1() # obj1.m1() # # obj2 = Class2() # obj2.m1() # to create a common object for multiple classes obj = [Class1(),Class2()] # for o1 in obj: # fun(o1) # obj.m1() obj[0].m1() ==================================== OVERLOADING =========== def met1(self): a+b def met1(self): a+b+c def met1(self): a+b+c+d ==> three types: 1) Operator Overloading 2) Method Overloading 3) Constructor Overloading Operator Overloading: ===================== + ==> sum + ==> string concatenation # Operator Overloading class Student: def __init__(self,m1,m2,m3): self.m1 = m1 self.m2 = m2 self.m3 = m3 def calci(self): self.total = self.m1 + self.m2 + self.m3 # addition result = "The Total of Student = "+str(self.total) # concatenation print(result) stu = Student(88,79,97) stu.calci() ================================================ # Operator Overloading class Calculator: def __init__(self,a,b): self.a = a self.b = b def m1(self): self.res = self.a + self.b return self.res def __add__(self,other): return self.res + other.res def __mul__(self, other): return self.res * other.res obj1 = Calculator(10,20) obj2 = Calculator(20,30) print(obj1.m1()) print(obj2.m1()) print(obj1 + obj2) print(obj1 * obj2) ================================== Assignment: ========= WAP in python to overload the less than and greater than operators. ======================================================= Method Overloading: =================== # Method Overloading # class methodOverload: # def m1(self): # print("With zero parameters.") # # def m1(self,a): # print("With one parameter") # # def m1(self,a,b): # print("With two parameters") # # mo = methodOverload() # # # mo.m1() # no arguments # # mo.m1(10) # mo.m1(10,20) class methodOverloading: def addition(self,a = None,b = None,c = None): if a != None and b != None and c != None: self.res = a + b+ c print("The Sum of three numbers = ",self.res) elif a != None and b != None: self.res = a + b print("The Sum of two numbers = ",self.res) elif a != None: self.res = a print("The Sum of one number = ",self.res) else: self.res = 0 print("The Sum is = ",self.res) mo = methodOverloading() mo.addition() # with 0 arguments mo.addition(10) # with 1 argument mo.addition(10,20) # with two arguments mo.addition(10,20,30) # with three arguments ===================================================== Constructor Overloading ======================== Constructor overloading is not possible in Python. # Constructor Overloading class Class1: def __init__(self): print("The Constructor with 0 parameters.") def __init__(self,a): print("The Constructor with 1 parameter.") def __init__(self,a,b): print("The Constructor with 2 parameters.") # obj = Class1() # obj = Class1(10) obj = Class1(10,20)