Types of Inheritance in Python

Inheritance in Python : 


     Reusing code is an important feature of any programming language(OOP). Reusing
Prewritten code has Manifolds merits. It not only saves effort to write a code and cost
for production but also enhance its reliability.  So the code written for previous software
will not be required to re-write ,re-debug ,  re-test  for new updates .

Types of Inheritance in Python


   The technique of creating new class from an existing class by accessing there attributes
and Methods are called as Inheritance.  The child classes are created by first inheriting
parent's class data and methods and then adding new specialized data and Methods
(function).In the whole process of inheritance Parent class will remain unchanged .
The concept of  inheritance is mainly used to implement 'is-a' relationship. for example




The existing class (old class) have different names .
1.Base Class
2.Parent class
3.Super Class

The New class is known as
1.Derived Class
2.Child class
3.sub class


  NOTE
  In class inheritance The " top-down " approach is used.



  NOTE
The subclass inherits all the Methods of Super class and adds extensions of its own.

Types of Inheritance :

   The way  by which a class inherits  from other class, Inheritance get divided into
four main types

1.Single Inheritance
2.Multiple Inheritance
3.Multi-level Inheritance
4.Multi-path Inheritance


1.Single Inheritance :

             In single inheritance a class can be derived from a single base class There are 
Only Two classes   One is parent and Other is Child . In which child class will access
all attributes of Parent class with the help of an object of child class.

Example of single Inheritance: 



class animal :
  def Name (self):
     print("Name of Animal is Dog")
class info(animal):
  def info(self):
     print(" color of that Dog is brown ")
obj = info()
obj.Name()
obj.info()

Output :


Name of Animal is Dog
color of Dog is brown 


Code written above is an example of single inheritance in which info is child class and
Inheriting from Parent class Animal . object of child class is able to access methods of
parent class .

2. Multiple inheritance : 

     When a subclass inherits from more than one class it is called multiple inheritance . 
A derived class can have  all the features of both the base classes and in addition to them 
derived class can has its own methods and attributes . 
  In multiple Inheritance  any specified attributes is first searched in the derived class
If it is not found there ,the search continues into parent classes using depth first technique.
i.e. " MRO " Method resolution Order (left to right ).

Syntax for Multiple Inheritance :



class B1:

   Statements Block 

Class B2:

    Statements Block

Class Derived (B1, B2):

   Statement block

Multiple Inheritance Flow chart 


3.Multilevel Inheritance : 

  The technique of deriving a class from an already derived class is called as
Multilevel Inheritance . Super class acts as a base for sub class 1 and sub class 1
is base(Parent class ) for sub class 2. So features(Methods ,Variables) of super class
can be access by subclass 2 . subclass1 is also called as intermediate base class because 
it provides a link  for inheritance between super class an subclass 2.so subclass 2
have features of superclass + subclass 1 +its own .

  NOTE
 "Number of levels can go up to any number i.e. Infinite "

Syntax of multilevel inheritance :


class B1:

   Statements Block 

Class B2(B1):

    Statements Block

Class B3 ( B2):

   Statement block


Multilevel Inheritance Flow chart :

EXAMPLE : 

class Person :
  def Name(self):
     print("Name of Person....")

class Teacher(Person):
  def qualification(self):
     print("Qualification of teacher")

class Principal(Teacher) :
  def Income(self):
     print("income of Principal ...")

obj = Principal()
obj.Name()
obj.qualification()
obj.Income()


Output :

Name of Person....
Qualification of teacher
income of Principal ...



In above example object of subclass 2 accessing methods of superclass as well as subclass1
In multilevel inheritance  any specific method or attribute  is first searched in the current class .
If it is not found there then subclass1 is searched. If it is not found there as well  the base class 
is searched . even if it is not there then object class is checked at the en .The all things are done 
by MRO Method resolution order .


  NOTE
 "Python have MRO and c3 algorithm to follow classes"



4. Multipath Inheritance :

             Defining a class from other derived classes which are having same Base class (Parent class)
is called as Multipath inheritance .



from above flow chart we can see that subclass 3 have two intermediate class i.e. subclass1 
and  subclass2 .Both these class are derived from same  parent class .subclass 3 have features
of superclass and subclasses . subclass3 inherits features of base class from two different paths.

Syntax :

class B1:

   Statements Block 

Class B2(B1):

    Statements Block

Class B3 ( B1):

   Statement block

Class B4(B2,B3) :
Statements block


 There is demerit of Multi-path inheritance called as Diamond Problem that derived class inherits 
features of super class twice. This results in the formation of duplicate members.   

So these are the types of Inheritance in python.