When creating a complex program, it is common to see certain features and characteristics repeated, and inheritance provided by object-oriented programming allows us to repeat the code. Inheritance allows the reusability of code which make the complex program easier to understand.
In this blog, we will discuss types of inheritance in Python in deep detail. Let's start going!
Inheritance is the property of Python through which the object can acquire all of a parent object's properties and behaviors. It is one of the most important Features of Object Oriented Programming
The parent-child relationship, also known as the IS-A relationship, is represented by inheritance. For Example, Children inherit some parent's properties such as face cuts and height.
Types of Inheritance in Python
There are five types of Inheritance in Python.
Single Inheritance
Multiple Inheritance
Multilevel Inheritance
Hierarchical Inheritance
Hybrid Inheritance
Single Inheritance
Single inheritance is one of the types of inheritance in Python, where there is only one base class and one child class. It is the inheritance type that is most frequently used.
In the diagram above, the downward arrow indicates that class B is descended from class A. This implies that class B inherits class A's members and functions designated as public or protected, enabling class B objects to access class A's methods and properties.
Example of Single Inheritance:
# Python program to demonstrate single inheritance
# Base class
class Father:
def function_1(self):
print ('I am father.')
# Child class
class Children(Father):
def function_2(self):
print ('I am son.')
object = Children()
object.function_1()
object.function_2()
Output:
Explanation:
In the above example, class children inherit the property and data of the father's class having inheritance relations.
When there are multiple levels of inheritance, the new derived class receives an additional inheritance from both the base class and the derived class. This type of inheritance in Python is referred to as multilevel inheritance.
In the above diagram, class C is descended from class B, which is from class A.
Example of Multilevel Inheritance:
# Python program to demonstrate multilevel inheritance
class A:
def __init__(self, aname):
self.aname = aname
# Intermediate class
class B(A):
def __init__(self, bname, aname):
self.bname = bname
# invoking constructor of Grandfather class
A.__init__(self, aname)
# Derived class
class C(B):
def __init__(
self,
cname,
bname,
aname,
):
self.cname = cname
# Invoking constructor of Father class
B.__init__(self, bname, aname)
def display(self):
print ('A name :', self.aname)
print ('B name :', self.bname)
print ('C name :', self.cname)
# Driver code
s1 = C('Rohit', 'Mohit', 'Lalit')
print (s1.aname)
s1.display()
Output:
Explanation:
In the above example, class B is inheriting the property of class A and class C is inheriting the class B property i.e. class C inheriting the property of class A also.
Hierarchical inheritance is the term used to describe situations with multiple derived classes from a single base class.
Here in the above block diagram, class A and B inherit the property of class C, which shows hierarchical inheritance.
Example of Hierarchical Inheritance:
# Python program to demonstrate Hierarchical inheritance
# Base class
class A:
def function_1(self):
print ('Parent of B and C')
# Derived class1
class B(A):
def function_2(self):
print ('Child class of A')
# Derivied class2
class C(A):
def function_3(self):
print ('Child class of A')
object1 = C()
object2 = B()
object1.function_1()
object1.function_3()
object2.function_1()
object2.function_2()
Output:
Explanation:
In the above example, class B and class C inherit the property of class A having a hierarchical relationship among them.
Hybrid Inheritance
It combines multiple inheritances with multilevel inheritance. A class can have two or more parent classes, but only one of them can have derived classes.
Here in the above block diagram, class D is derived from classes B and C, whereas class B is inherited from class A.
Example of Hybrid Inheritance:
# Python program to demonstrate hybrid inheritance
class Office:
def func1(self):
print ('This function is in Office.')
class Emp1(Office):
def func2(self):
print ('This function is in Employee 1.')
class Emp2:
def func3(self):
print ('This function is in Employee 2.')
class Emp3(Emp1, Emp2):
def func4(self):
print ('This function is in Employee 3.')
# Driver's code
object = Emp3()
object.func1()
object.func2()
Output:
Explanation:
In the above example, class B is inheriting the property of class A, and class D inherits the property of class B and class C.
Inheritance encourages codes' reusability, which increases trustworthiness and development, and maintenance costs are reduced. It makes it easier to create class libraries.
Why is inheritance used in Python?
By using inheritance, we can create a class that contains all the methods and attributes of another class. The class being inherited from, also known as the base class, is the parent class. The class that inherits from another base class is referred to as a child class or derived class.
How many types of inheritances are there in Python?
The five types of inheritance in Python are single, multiple, multilevel, hierarchical, and hybrid inheritance.
What is the significance of inheritance in Python?
Application development and maintenance are made simpler by inheritance, which enables us to identify a class within another class. This permits quick implementation times and the reuse of code functionality.
Is Python object-oriented?
Yes, Python is an object-oriented programming language.
Conclusion
Congratulations on finishing the blog! We have discussed the types of inheritance in Python. Further, we have discussed each types of inheritance in Python with the help of examples and block diagrams.
We hope this blog has helped to enhance your knowledge of types of inheritance in Python. Do not stop learning! We recommend you read some of our articles related to Python: