Polymorphism: ============= poly ==> many morphs ==> ways one thing in multiple ways one form with multiple definitions is called as "poly morphism". # overriding: ============= def fun(a,b): return a+b def fun(a, b, c): return a*b*c # overloading ============== def f1(a,b): return a+b def f1(a,b,c): return a+b+c ex: x-bank: def personal_loan(): roi = 0% def home_loan(): roi = 0% def car_loan(): roi = 0% icici = x-bank() icici.personal_loan(): roi = 10% ==> polymorphism can be implemented in two ways: 1) Overloading operator overloading method overloading constructor overloading 2) Overriding method overriding constructor overriding operator overloading ==================== a = 10 b = 20 c = a + b # 30 addition d = a * b # 200 multiplication s1 = "a" s2 = "b" s3 = s1 + s2 # "ab" concatenation/joining s4 = s1 * 3 # "aaa" repetition of string =============================================== # Operator Overloading # + operator can be overloaded? # + = numbers (addition) or string (concatenation) # + = object? class Book: def __init__(self,pages): self.pages = pages def __add__(self, other): return self.pages + other.pages def __sub__(self, other): return self.pages - other.pages def __mul__(self, other): return self.pages * other.pages def __lt__(self, other): return self.pages < other.pages b1 = Book(100) b2 = Book(200) # print(b1 + b2) # Type Error (when + operator was not overloaded) print("The Total number of pages in a book = ",b1 + b2) print(b2 - b1) print(b1 * b2) print(b1 < b2) ========================================================================== + ==> __add__(self,other) - ==> __sub__(self,other) * ==> __mul__(self, other) / ==> __div__(self, other) // ==> __floordiv__(self, other) % ==> __mod__(self, other) ** ==> __pow__(self, other) += ==> __iadd__(self, other) -= ==> __isub__(self, other) *= ==> __imul__(self, other) /= ==> __idiv__(self, other) //= ==> __ifloordiv__(self, other) %= ==> __imod__(self, other) **= ==> __ipow__(self, other) < ==> __lt__(self, other) <= ==> __le__(self, other) > ==> __gt__(self, other) >= ==> __ge__(self, other) == ==> __eq__(self, other) != ==> __ne__(self, other) ============================================ # WAP TO OVERLOAD MULTIPLICATION OPERATOR TO WORK ON EMPLOYEE OBJET class Employee: def __init__(self,name, salary): self.name = name self.salary = salary def __mul__(self, other): return self.salary * other.days class timeSheet: def __init__(self,name,days): self.name = name self.days = days e = Employee('Arjun',4500) t = timeSheet('Arjun',30) print("The Arjun's salary of the september month = ",e * t) ========================================================== Method Overloading: =================== # method overloading # fun() ==> sum on 0, 1, 2, 10, 100, 200 class Calculator: def sum(self,a = None,b = None,c = None): if a != None and b != None and c != None: print("The Sum of Three numbers = ",a+b+c) elif a != None and b != None: print("The Sum of Two Numbers = ",a+b) elif a!=None: print("The Sum of one number = ",a) else: print("Kindly provide at least one argument for proceeding.") calsi = Calculator() calsi.sum() calsi.sum(100) calsi.sum(100,200) calsi.sum(100,200,300) # calsi.sum(100,200,300,400) ======================================== # method overloading class Calculator: def sum(self,*n): total = 0 for i in n: total += i return total cal = Calculator() print("The Sum = ",cal.sum()) # 0 parameters print("The sum = ",cal.sum(100)) # 1 parameter print("The Sum = ",cal.sum(100,300,500)) # 3 print("The Sum = ",cal.sum(100,200,300,400,500)) # 5 ====================================== Constructor Overloading: ======================== Python does not support the constructor Overloading. # Constructor Overloading class myClass: def __init__(self): print("Hi") def __init__(self,name): print("Hi",name) mc = myClass("Ravi")