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 .
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
2.Child class
3.sub class
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.
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:
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 :
Multiple Inheritance Flow chart
3.Multilevel Inheritance :
The technique of deriving a class from an already derived class is called asMultilevel 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 .
Syntax of multilevel inheritance :
Output :
Name of Person....
Qualification of teacher
income of Principal ...
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 .
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 :
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.