IKH

Types of Inheritance

Single Inheritance

  • The concept of inheriting the properties from one class is known as single inheritance.

Example

Python
class P: 
def m1(self): 
print("Parent Method") 
class C(P): 
def m2(self): 
print("Child Method") 
c=C() 
c.m1() 
c.m2() 

Output

PowerShell
Parent Method
Child Method

Multi Level Inheritance

The concept of inheriting the properties from multiple classes to single classes to single class with the concept of one after another is known as multilevel inheritance.

Example

Python
class P: 
def m1(self): 
print("Parent Method") 
class C(P): 
def m2(self): 
print("Child Method") 
class CC(C): 
def m3(self): 
print("Sub Child Method") 
c=CC() 
c.m1() 
c.m2() 
c.m3()

Output

PowerShell
Parent Method
Child Method
Sub Child Method

Hierarchical Inheritance

The concept of inheriting properties from one class into multiple classes which are present at same level is known as hierarchical Inheritance.

Example

Python
class P: 
def m1(self): 
print("Parent Method") 
class C1(P): 
def m2(self): 
print("Child1 Method") 
class C2(P): 
def m3(self): 
print("Child2 Method") 
c1=C1() 
c1.m1() 
c1.m2() 
c2=C2() 
c2.m1() 
c2.m3() 

Output

PowerShell
Parent Method
Child1 Method
Parent Method
Child2 Method

Multiple Inheritance

The concept of inheriting the properties from multiple classes into a single class at a time is known as multiple inheritance.

Example

Python
class P1: 
def m1(self): 
print("Parent1 Method") 
class P2: 
def m2(self): 
print("Parent2 Method") 
class C(P1,P2): 
def m3(self): 
print("Child2 Method") 
c=C() 
c.m1() 
c.m2() 
c.m3() 

Output

PowerShell
Parent1 Method
Parent2 Method
Child2 Method
If the same method is inherited from both parent classes,then Python will always consider the 
order of Parent classes in the declaration of the child class.
class C(P1,P2): ===>P1 method will be considered
class C(P2,P1): ===>P2 method will be considered

Example

Python
class P1: 
def m1(self): 
print("Parent1 Method") 
class P2: 
def m1(self): 
print("Parent2 Method") 
class C(P1,P2): 
def m2(self): 
print("Child Method") 
c=C() 
c.m1() 
c.m2() 

Output

PowerShell
Parent1 Method
Child Method

Hybrid Inheritance

  • Combination of Single, multi level, multiple and hierarchical inheritance is known a hybrid inheritance.

Cyclic Inheritance

The concept of inheriting properties from one class to another class in cyclic way, is called cyclic inheritance. Python wont support for cyclic inheritance of course it is really not required.

Example

Python
class A(A):pass
NameError: name 'A' is not defined

Output

PowerShell
2

Example

Python
class A(B): 
pass 
class B(A): 
pass 
NameError: name 'B' is not defined.

Output

PowerShell
7

Method Resolution Order (MRO)

  • In Hybrid Inheritance the method resolution order is decided based on MRO algorithm.
  • This algorithm is also known as C3 algorithm.
  • Samuele Pedroni proposed this algorithm.
  • It follows DLR (Depth First Left to Right)
  • i.e Child will get more priority than Parent.
  • Left Parent will get more priority than Right Parent
  • MRO(X)=X+Merge(MRO(P1),MRO(P2),…,ParentList)

Head Element vs Tail Terminology

  • Assume C1,C2,C3,…are classes.
  • In the list : C1C2C3C4C5….
  • C1 is considered as Head Element and remaining is considered as Tail.

How to find Merge

  • Take the head of first list If the head is not in the tail part of any other list,then add this head to the result and remove it from the lists in the merge.
  • If the head is present in the tail part of any other list,then consider the head element of the next list and continue the same process.

Note

  • We can find MRO of any class by using mro() function.
  • print(ClassName.mro())

Demo Program-1 for Method Resolution Order

  • mro(A)=A,object
  • mro(B)=B,A,object
  • mro(C)=C,A,object
  • mro(D)=D,B,C,A,object

test.py

Example

Python
class A:pass 
class B(A):pass 
class C(A):pass 
class D(B,C):pass 
print(A.mro()) 
print(B.mro()) 
print(C.mro()) 
print(D.mro()) 

Output

PowerShell
[<class '__main__.A'>, <class 'object'>]
[<class '__main__.B'>, <class '__main__.A'>, <class 'object'>]
[<class '__main__.C'>, <class '__main__.A'>, <class 'object'>]
[<class '__main__.D'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.A'>, <class 
'object'>]

Demo Program-2 for Method Resolution Order

  • mro(A)=A,object
  • mro(B)=B,object
  • mro(C)=C,object
  • mro(X)=X,A,B,object
  • mro(Y)=Y,B,C,object
  • mro(P)=P,X,A,Y,B,C,object

Finding mro(P) by using C3 algorithm

Formula

  • MRO(X)=X+Merge(MRO(P1),MRO(P2),…,ParentList)
  • mro(p)= P+Merge(mro(X),mro(Y),mro(C),XYC)
  • = P+Merge(XABO,YBCO,CO,XYC)
  • = P+X+Merge(ABO,YBCO,CO,YC)
  • = P+X+A+Merge(BO,YBCO,CO,YC)
  • = P+X+A+Y+Merge(BO,BCO,CO,C)
  • = P+X+A+Y+B+Merge(O,CO,CO,C)
  • = P+X+A+Y+B+C+Merge(O,O,O)
  • = P+X+A+Y+B+C+O

Example

Python
class A:pass 
class B:pass 
class C:pass 
class X(A,B):pass 
class Y(B,C):pass 
class P(X,Y,C):pass 
print(A.mro())#AO 
print(X.mro())#XABO 
print(Y.mro())#YBCO 
print(P.mro())#PXAYBCO 

Output

PowerShell
[<class '__main__.A'>, <class 'object'>]
[<class '__main__.X'>, <class '__main__.A'>, <class '__main__.B'>, <class 'object'>]
[<class '__main__.Y'>, <class '__main__.B'>, <class '__main__.C'>, <class 'object'>]
[<class '__main__.P'>, <class '__main__.X'>, <class '__main__.A'>, <class '__main__.Y'>, <class 
'__main__.B'>,
<class '__main__.C'>, <class 'object'>]

Example

Python
class A: 
def m1(self): 
print('A class Method') 
class B: 
def m1(self): 
print('B class Method') 
class C: 
def m1(self): 
print('C class Method') 
class X(A,B): 
def m1(self): 
print('X class Method')
class Y(B,C): 
def m1(self): 
print('Y class Method') 
class P(X,Y,C): 
def m1(self): 
print('P class Method') 
p=P() 
p.m1()

Output

PowerShell
P class Method
In the above example P class m1() method will be considered.If P class does not contain m1() 
method then as per MRO, X class method will be considered. If X class does not contain then A 
class method will be considered and this process will be continued.
The method resolution in the following order:PXAYBCO

Demo Program-3 for Method Resolution Order

  • mro(o)=object
  • mro(D)=D,object
  • mro(E)=E,object
  • mro(F)=F,object
  • mro(B)=B,D,E,object
  • mro(C)=C,D,F,object
  • mro(A)=A+Merge(mro(B),mro(C),BC)
  • =A+Merge(BDEO,CDFO,BC)
  • =A+B+Merge(DEO,CDFO,C)
  • =A+B+C+Merge(DEO,DFO)
  • =A+B+C+D+Merge(EO,FO)
  • =A+B+C+D+E+Merge(O,FO)
  • =A+B+C+D+E+F+Merge(O,O)
  • =A+B+C+D+E+F+O

Example

Python
class D:pass 
class E:pass 
class F:pass 
class B(D,E):pass 
class C(D,F):pass 
class A(B,C):pass 
print(D.mro()) 
print(B.mro()) 
print(C.mro()) 
print(A.mro()) 

Output

PowerShell
[<class '__main__.D'>, <class 'object'>]
[<class '__main__.B'>, <class '__main__.D'>, <class '__main__.E'>, <class 'object'>]
[<class '__main__.C'>, <class '__main__.D'>, <class '__main__.F'>, <class 'object'>]
[<class '__main__.A'>, <class '__main__.B'>, <class '__main__.C'>, <class '__main__.D'>, <class 
'__main__.E'>,
<class '__main__.F'>, <class 'object'>]

Report an error