Types of Inheritance

 

Types of inheritance

Inheritance provides reusability and on the basis of how the classes are accessed there are main two types of inheritance.

1.      Single inheritance

2.      Multiple inheritance

1.     Single inheritance:

In single inheritance there is one base class. When one or more classes are derived from single base class is called single inheritance.

 

 

 



In above example class B and class C are inherited from class A and it is called single inheritance.

 

Example:

'''program of inheritance

Types of inheritance - single inheritance'''

 

class base:

    b1=100;

    @classmethod

    def printb1val(cls):

        print("b1 value is",cls.b1)

class der1(base):

    d1=50

    @classmethod

    def printd1val(cls):

        print("d1 value is",cls.d1)

    def printd1_b1val(cls):

        print("d1 and b1 value is",(base.b1+der1.d1))

 

d1=der1()

d1.printb1val()

d1.printd1val()

d1.printd1_b1val()

    Output:

 

('b1 value is', 100)

('d1 value is', 50)

('d1 and b1 value is', 150)

 

 

2.     Multiple inheritance

When a sub class is derived from more than one class is called multiple inheritance. In multiple inheritance there can be more than one super class and more than one sub class.

 

 

 

 

 

 



 Example:

'''program of inheritance types of inheritance multiple inheritance'''

class A:   

    def printa(self):

        self.a=10

        print(self.a)

class B:

  

    def printb(self):

        self.b=20

        print(self.b)

 

class C(A,B):

    def printc(self):

        self.c=30

        print(self.c)

 

c1=C()

c1.printa()

c1.printb()

c1.printc()             

Output:

10

20

30

Problems in multiple inheritance:

In multiple inheritance, in above example class C is inherited from A and B.  here when an object of class  C is created it call the constructor of class C. Here is the super class constructor to be invoked then,        super().__init__() should be written in class C.

If        class C(A,B):

 Is there then it will invoke class C and only class A constructor. It is not invoking the constructor of class B, to solve this problem super().__init__() should be written in every class.

The constructor of derived class is invoked then the left side base class constructor only invoked.

Class C(A,B) will call the class C constructor and then it search for base class constructor. The base class A constructor is available then it will be called but if class A does not have constructor then only it will call constructor of class B.

Class C(B,A) will call the constructor of class C and class B.

 

Example:

'''Problems in multiple inheritance'''

class A:

    def __init__(self):

        self.a=10

        super().__init__()

 

    def printa(self):

        print(self.a)

class B:

    def __init__(self):

        self.b=20

        super().__init__()           

       

    def printb(self):

        print(self.b)

 

 

class C(A,B):

    def __init__(self):

        self.c=30

        super().__init__()

 

    def printc(self):

        print(self.c)

 

c1=C()

c1.printa()

c1.printb()

c1.printc()

Method Resolution order:

'''program of inheritance

problems in multiple inheritance

Mehtod resolution order(MRO)'''

In multiple inheritance the derived class method is called and then base class constructor is called. If multiple inheritance is there and method is called then first of all the method will be searched in the current class and if the method is not found in the current class then it will search in the parent class in left to right order. It will never search the same class twice. This method of searching is called MRO- Method Resolution Order.

In MRO following are important points:

1.      The method will be searched first in the current (derived) class. if the method is not found in the derived class then only it will be searched in the base class.

2.      When there is multiple inheritance, the classes are searched from left to right order.

For example if class C(A,B) then class method searched first in class C. if it is not found in class C then it will be search class A.  if the method is not found in class A then only it will search in class B.

3.      The method will be searched in every class, once only.

If there is multiple inheritance and we want to identify that, in which fashion the methods are invoked then, mro() method is used.

          Classname.mro()

Provides the classes and its sequence of execution.

 

class A:

    def printdata(self):

        self.a=10

        print(self.a)

        super().printdata()

class B:

    def printdata(self):

        self.b=20

        print(self.b)

       

class C(A,B):

    def printdata(self):

        self.c=30

        super().printdata()

        print(self.c)

 

c1=C()

c1.printdata()

Output:

10

20

30