Hierarchy Inheritance: ====================== Single parent class and two or more child classes Polymorphism: ============= poly ==> many morphs ==> ways one thing can be implemented in may ways is called as "Polymorphism". ==> Polymorphism can be implemented in two mechanisms: 1) using Method Overriding =========================== class Class1: def m1(): print("Hi") class Class2(Class1): def m1(self): print("Hello") ==> In method overriding, the signature of the method (method head) is remains same but the implementation of the method (method body) can be differ. class ClassA: def m1(self,name): print("Hi",name) class ClassB(ClassA): def m1(self,name): print("Good night",name) class BankSystem:# parent class def rateOfInterest(self): roi = 0 return roi # HomeLoan ==> roi = 9 # Car Loan ==> roi = 15 class HomeLoan(BankSystem): def greets1(self): print("Welcome to XYZ Bank.") print("Follow these instructions for the Home Loan.") def rateOfInterest(self): roi = 9 return roi class CarLoan(BankSystem): def greets2(self): print("Welcome to XYZ Bank.") print("Follow these instructions for the Car Loan.") def rateOfInterest(self): roi = 15 return roi hl = HomeLoan() cl = CarLoan() hl.greets1() print(hl.rateOfInterest()) cl.greets2() print(cl.rateOfInterest()) Note: ===== Constructor Overriding is not possible. super(): ======= ==> can be used to invoke the parent class constructor implementation from the child class. Syntax: super().__init__() 2) using overloading ===================== class ClassA: def m1(selfsame): c = a + b return c class ClassB(ClassA): def m1(self,a,b,c): d = a+ b+ c return d class Calculator: def addition(self,a,b): return a+b def multiplication(self,a,b): return a*b class Calci(Calculator): def addition(self,a,b,c): return a+b+c def multiplication(self,a,b,c): return a*b*c cl = Calci() print(cl.addition(10,20,30)) ==================================================== Hybrid Inheritance: ==================== class C1:# Parent to C2 Class def m1(self): print("Hi") class C2(C1): # Child To C1 Class def m2(self): print("Hello") class C3(C2): def m3(self): print("Good Evening.") class C4(C2): def m4(self): print("Good Night") c4 = C4() c4.m4() # c4.m3() c4.m2() c4.m1() =============================================================== Exception Handling: =================== Error ==> Mistake in a program Error can stop the program from execution. ==> Three types of errors: 1) Syntax Errors ==> Program/statement with incorrect syntax 2) Run-time errors/Exception ==> Based on the type of input during the run time str1 = "Python" print(str1[0]) print(str1[1]) print(str1[2]) print(str1[3]) print(str1[4]) print(str1[5]) print(str1[6]) print("Program is success.") 3) Logical Errors ================= ==> the application cannot provide the output as per our expectation because of logical error. class BankingSystem: def greets(self,time): if time >= 0 and time < 12: print("Good Afternoon.") elif time >= 12 and time < 16: print("Good Afternoon.") elif time >= 16 and time < 20: print("Good Evening.") else: print("Good night.") bs = BankingSystem() bs.greets(6) Exception Handling: ================== when an exception can occur, PVM can stop the program immediately We want to make execute the program normally even an exception can be occurred. So, we need Exception Handling. User ==> BankATM deposit withdraw(): if amount <= balance: withdraw can be successful. else: withdraw can be fail. calculator a/b 10/0 ==> Exception ==> To handle the exception: try ==> keyword define the logic which possible with an exception. except a = int(input("Enter a:")) b = int(input("Enter b:")) try: c = a/b # Exception print(c) except: print("Dividing a number with 0 is not possible in real-life") ======================================================== a = int(input("Enter a:")) b = int(input("Enter b:")) try: c = a/b # Exception print(c) except ZeroDivisionError: print("Dividing a number with 0 is not possible in real-life") ==================================================================== a = int(input("Enter a:")) b = int(input("Enter b:")) try: c = ((a+b) / (a-b)) except NameError: print("Name Error is Observed because of name mistake.") except ZeroDivisionError: print("Dividing a number with 0 is not possible in real-life") else: print(c)