Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is Inheritance in Python?
3.
Types of Inheritance in Python
3.1.
Single Inheritance
3.1.1.
Example of Single Inheritance:
3.1.2.
Explanation: 
3.2.
Multiple Inheritance
3.2.1.
Example of Multiple Inheritance:
3.2.2.
Explanation:
3.3.
Multilevel Inheritance
3.3.1.
Example of Multilevel Inheritance:
3.3.2.
Explanation:
3.4.
Hierarchical Inheritance
3.4.1.
Example of Hierarchical Inheritance:
3.4.2.
Explanation:
3.5.
Hybrid Inheritance
3.5.1.
Example of Hybrid Inheritance:
3.5.2.
Explanation:
4.
Frequently Asked Questions
4.1.
What are the advantages of inheritance? 
4.2.
Why is inheritance used in Python?
4.3.
How many types of inheritances are there in Python?
4.4.
What is the significance of inheritance in Python?
4.5.
Is Python object-oriented?
5.
Conclusion
Last Updated: May 4, 2024
Easy

Types of Inheritance in Python

Author Ayush Mishra
1 upvote

Introduction

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!

Types of Inheritance in Python

Must Recommended Topic, Floor Division in Python and Python Round Function.

What is Inheritance in Python?

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

Inheritance in python

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.

  1. Single Inheritance
  2. Multiple Inheritance
  3. Multilevel Inheritance
  4. Hierarchical Inheritance
  5. Hybrid Inheritance
types of 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.

single Inheritance

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:

Output of Single Inheritance

Explanation: 

In the above example, class children inherit the property and data of the father's class having inheritance relations.

Know about Single Inheritance in Java in detail.

Multiple Inheritance

Multiple inheritances are another types of inheritance in Python, which refer to deriving a class from multiple base classes.

Multiple Inheritance

In the diagram above, class C comes from classes A and B. Classes A and B's attributes and operations will be present in class C.

Example of Multiple Inheritance:

# Python program to demonstrate multiple inheritances

# Base class1
class A:

    aname = ''

    def aclass(self):
        print (self.aname)

# Base class2

class B:

    bname = ''

    def bclass(self):
        print (self.bname)

# Child class

class C(A, B):

    def cname(self):
        print ('B :', self.bname)
        print ('A :', self.aname)

s1 = C()
s1.bname = 'Mohit'
s1.aname = 'Ashutosh'
s1.cname()


Output:

Output of Multiple Inheritance

Explanation:

In the above example, class C is inheriting the property of class B and class A having a multiple inheritance relationship among us.

Also read, Four Pillars of OOPS

Multilevel Inheritance

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.

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:

Output of Multilevel inheritance

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.

You can compile it with online python compiler.

Hierarchical Inheritance

Hierarchical inheritance is the term used to describe situations with multiple derived classes from a single base class.

Hierarchical Inheritance

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:

Output of Hierarchical Inheritance

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.

Hybrid Inheritance

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:

Output of hybrid inheritance

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.

Check out this article - Quicksort Python and What is Object in OOPs.

Frequently Asked Questions

What are the advantages of inheritance? 

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: 

 

You can also consider our paid courses such as DSA in Python to give your career an edge over others!

We wish you Good Luck! 

Happy Learning!

Live masterclass