Inheritance: ============ phonepe{ send money { mobile upi account { } check balance { implementation } } ex: p1 =====> p2 phonepe mobile { mobile amount : 10000 upi proceed check balance } Inheritance: ============ Acquiring of data and methods from one class to another class is called as "Inheritance". Use: ==== reusability of code can be increased. 4-types: 1) Single Inheritance ==> 1-1 inheritance class Parent: def m1(self): print("Hello Parent.") class Child(Parent): def m2(self): print("Hello Child.") po = Parent() co = Child() po.m1() co.m2() co.m1() # po.m2() ==> Here: the object of child class, can inherit the data from both parent class and child class also. Whereas, the object of parent class can inherit the data from only parent class (cannot from child class). 2) Multiple Inheritance ==> many to one ==> In this, there are two independent parent classes (not inherit to each other) and one child for both parent classes. ==> in this case, the object of child can inherit the data from parent-1, parent-2 and from itself. Whereas, the object of parent-1 can access the data of itself and same to the parent2 also. class Parent1: def __init__(self,a,b): self.a = a self.b = b def addition(self): self.summation = self.a + self.b print("The Sum of Two numbers = ",self.summation) class Parent2: def __init__(self,a,b): self.a = a self.b = b def subtraction(self): self.difference = self.a - self.b print("The Difference of two numbers = ",self.difference) class Child(Parent1,Parent2): def printing(self): print("The Results of Sum and Difference are:") # parent class objects # po1 = Parent1(100,200) # po2 = Parent2(200,300) # object of child class co = Child(1000,2000) co.printing() co.addition() co.subtraction() # parent classes are independent together po1 = Parent1(200,100) po1.addition() # po1.printing() # po1.subtraction() 3) Multilevel Inheritance ==> Grand Parent class ==> Parent Class ==> Child Class the object of child class, can inherit the data from: itself, parent and from Grand Parent class also. the object of parent class, can inherit the data from itself and from grand Parent class. the object of grand parent class, can inherit from its own. class Parent1: def f1(self): print("Good Morning.") class Parent2(Parent1): def f2(self): print("Good Afternoon.") class Child(Parent2): def f3(self): print("Good Evening.") co = Child() co.f3() co.f2() co.f1() p1o = Parent2() p1o.f2() p1o.f1() 4) Hierarchy Inheritance ================================================== Overriding: =========== m1(): adding m1(): product m1(): sub 1) Method Overriding # class A: # def f1(self): # print("The Method f1 in Class 'A'") # # def f2(self): # print("The Method f2 in Class 'B'") # # class B(A): # def f1(self): # print("I am Redefining the method f1 in Child Class B") # # def f2(self): # print("I am Redefining the method f2 in Child Class B") # # # child = B() # # child.f1() # # # # A().f1() # # if(__name__ == "__main__"): # child = B() # # child.f2() # class parent: def f1(self): print("Hi") def f1(self): print("Hello") def f1(self): print("Have a Great day.") po = parent() po.f1() =========================== 2) Constructor Overriding