Inheritance: ============ acquiring of the properties (attributes & methods) from one class to another class is called as an "Inheritance". or Inherit the data and methods from one class to another class is called as an "Inheritance" ex: class Bank: def __init__(self,roi = 0): self.roi = roi def homeLoan(self): self.roi = 0 class ICICI(Bank): self.roi = 10 homeLoan(): self.roi =7 class Axis(Bank): self.roi = 12 homeLoan(): self.roi = 7.9 ===================== Types of Inheritance: ====================== 1) Single Inheritance ====================== there is only one parent class and only one child class ==> the child class can acquire all the data from the parent class but the parent cannot acquire the data from its child class. class Parent: a = 121 b = 1.001 def sum(self): print("The Sum is = ",self.a + self.b) class Child(Parent): p = 112 q = 1.112 def sub(self): print("The Difference is = ",self.p - self.q) pobj = Parent() print(pobj.a) pobj.sum() cobj = Child() cobj.sub() cobj.sum() print("Parent Class Attributes are:") print(cobj.a) print(cobj.b) print("Child class Attributes are:") print(cobj.p) print(cobj.q) # print(pobj.p) ========================================== 2) Multiple Inheritance ======================= Single child class can acquire the data and methods (properties) from multiple independent classes. class Parent1: a = 121 b = 111 class Parent2: def Power(self,a,b): print("Power is = ",a**b) class Child(Parent1,Parent2): p = 1221 q = 1.2221 def sum(self): print("The Sum is = ",self.p + self.q) ch = Child() ch.sum() ch.Power(3,4) print("The Parent1 Attributes are = ") print(ch.a) print(ch.b) p1 = Parent1() # p1.sum() # p1.Power(2,5) ================================================== 3) Multilevel Inheritance ========================= The features of base class and derived class can inherit to another derived class. class A: a = 10 b = 20 class B(A): p = 100 q = 200 class C(B): x = 111 y = 222 cobj = C() print("The Data of itself = ") print(cobj.x,cobj.y) print("The Data from B Class = ") print(cobj.p,cobj.q) print("The Data from A Class = ") print(cobj.a,cobj.b) bobj = B() print("The Data from B Class = ") print(bobj.p,bobj.q) print("The Data from A Class = ") print(bobj.a,bobj.b) # print(bobj.x) ============================================== 4) Hierarchy Inheritance ========================= ==> single parent class with multiple independent child classes ==> all child classes can acquire the data from parent class equally. class A: def greets(self): print("Hi") class B(A): def m1(self): print("Good Morning.") class C(A): def m2(self): print("Good After Noon.") class D(A): def m3(self): print("Good Evening.") class E(A): def m4(self): print("Good Night.") bobj = B() bobj.greets() bobj.m1() # bobj.m2() cobj = C() cobj.greets() cobj.m2() dobj = D() dobj.greets() dobj.m3() eobj = E() eobj.greets() eobj.m4() =========================== 5) Hybrid Inheritance ====================== combination of above any type of inheritance Single + multiple multiple + multilevel hierarchy + single + multiple class A: class B(A): class C: class D(B,C):