What are the Different Types of Inheritance in Python?

What are the different types of inheritance?

Are you exploring 💡 the object-oriented concepts of Python, such as inheritance and the different types of inheritance in Python?

Inheritance is an advanced concept of object-oriented programming paradigms. It allows us to inherit or extend the properties and functions from one class to another, ultimately improving the program’s behavior in terms of space and speed. 

In this article, we’ll discuss inheritance, how it works, and the different types of inheritance in Python. There are two main classes in inheritance: a base class, also known as a parent class, and the second class, called a derived class, also known as a child class. 

To learn more about inheritance and how it works, let’s dive deep into it!

 

 

 

What is Inheritance?

Inheritance is the process of inheriting properties and functionalities of the base class into a derived class, known as inheritance. Inheritance is one of the powerful features of object-oriented programming that allows you to inherit one class’s pre-built properties and functionalities to another. 

The syntax of inheritance of pretty simple

# base class (parent class)

Class A():

# body of class A




# derived class (child class)

Class B(A):

# body of class B

 

 

What Are the Different Types of Inheritance in Python?

There are different types of inheritance in Python; each has a specific reason to use it according to the nature of the software problem. These different types of inheritance are the following so let’s explore each.

  1. Single Inheritance
  2. Multilevel Inheritance
  3. Multiple Inheritance
  4. Hierarchical Inheritance
  5. Hybrid Inheritance

 

Single Inheritance in Python

Single inheritance is one of the most common types of inheritance in Python, and it has only a single base class and a derived class. Therefore, the derived class has the authority to access the properties and functions of the base class.

Types of inheritance: Single inheritance

Code 

# base class

class A:

    def funcA(self):

        print("This function is in the parent class.")




        

# Derived class 

class B(A):

    def funcB(self):

        print("This function is in the child class.")

        

        

# Driver's code

b = B()

b.funcA()

b.funcB()


 

Output

This function is in the parent class.

This function is in the child class.

 

 

Multilevel Inheritance in Python

Multilevel inheritance is an advanced version of single inheritance, and with multilevel inheritance, there are endless possibilities for reusing class properties and functionalities. However, there isn’t any limit to the number of derived classes that can inherit the functionalities.

Types of inheritance: Multilevel inheritance

Code

# base class

class A():

    def funcA(self):

        print("This is class A.")




        

# Derived class 

class B(A):

    def funcB(self):

        print("This is class B.")

        

        

# Derived class 

class C(B):

    def funcC(self):

        print("This is class C.")

        




# Derived class 

class N(C):

    def funcN(self):

        print("This is class N.")

        

# creae object of class N

n = N()




n.funcA()

n.funcB()

n.funcC()

n.funcN()

 

Output

This is class A.

This is class B.

This is class C.

This is class N.

 

 

Multiple Inheritance in Python

Multiple inheritances in Python allow you to inherit more than one base class into a derived class at a time. This improves code reusability by inheriting the already-defined functionalities instead of recreating them. We are open to inheriting the functionalities of different classes into a single class in multiple inheritances.

Types of inheritance: Multiple inheritance

 

Code

# base class

class A():

    def funcA(self):

        print("This is class A.")




        

# base class 

class B():

    def funcB(self):

        print("This is class B.")

        

        

# base class

class C():

    def funcC(self):

        print("This is a class C.")

        




# Derived class inheriting class "A. B and C"

class DerivedClass(A,B,C):

    def funcD(self):

        print("This is a derived class.")

        

# creae object of class derived class 'DerivedClass'

d = DerivedClass()




d.funcA()

d.funcB()

d.funcC()

d.funcD()


Output

This is class A.

This is class B.

This is a class C.

This is a derived class.

 

 

Hierarchical Inheritance in Python

Hierarchical inheritance is the opposite of multiple inheritance; there can be numerous derived classes inheriting a single base class. Therefore, hierarchical inheritance is a good structure for scenarios where the workflow uses a hierarchical structure.

Types of Inheritance: Hierarchichal inheritance

Code

# base class

class BaseClass():

    def funcBase(self):

        print("This is Base Class.")




        

# derived class 

class A(BaseClass):

    def funcA(self):

        print("This is derived class A.")

        

        

# derived class

class B(BaseClass):

    def funcB(self):

        print("This is a derived class B.")

        




# derived class

class C(BaseClass):

    def funcC(self):

        print("This is a derived class C.")

        

# creae objects of class derived classes

a = A()

b = B()

c = C()




# calling functions

print("****** A ******")

a.funcBase()

a.funcA()




print("\n****** B ******")

b.funcBase()

b.funcB()




print("\n****** C ******")

c.funcBase()

c.funcC()


 

Output

****** A ******

This is Base Class.

This is derived class A.




****** B ******

This is Base Class.

This is a derived class B.




****** C ******

This is Base Class.

This is a derived class C.

 

 

Hybrid Inheritance in Python

Hybrid inheritance combines single, hierarchical, multiple, and multilevel types. Unfortunately, these aren’t hard and fast rules for hybrid inheritance. You can customize it according to your needs. It is suitable for hierarchical structures like Payroll, HR models, organizational hierarchy, etc.

Types of Inheritance: Hybrid inheritance

Code

# base class

class BaseClass():

    def funcBase(self):

        print("This is Base Class.")




# base n derived class

class Alpha(BaseClass):

    def alpha(self):

        print("This is Class Alpha.")




        

# derived class 

class A(Alpha):

    def funcA(self):

        print("This is derived class A.")

        

        

# derived class

class B(Alpha):

    def funcB(self):

        print("This is a derived class B.")

        




# derived class

class C(Alpha):

    def funcC(self):

        print("This is a derived class C.")

        

# creae objects of class derived classes

a = A()

b = B()

c = C()




# calling functions

print("****** A ******")

a.funcBase()

a.alpha()

a.funcA()




print("\n****** B ******")

b.funcBase()

b.alpha()

b.funcB()




print("\n****** C ******")

c.funcBase()

c.alpha()

c.funcC()


 

Output

****** A ******

This is Base Class.

This is Class Alpha.

This is derived class A.




****** B ******

This is Base Class.

This is Class Alpha.

This is a derived class B.




****** C ******

This is Base Class.

This is Class Alpha.

This is a derived class C.

 

 

Conclusion

To summarize this article on the different types of inheritance, we have discussed inheritance and the various types of inheritance in Python, along with its practical implementation or examples. Inheritance allows us to reuse the defined code in various parts of the program.

Inheritance creates relationships between different independent classes to share data.

Let’s have a quick recap of the topics discussed in this article

  1. What is inheritance in Python?
  2. What are the different types of inheritance in Python?
  3. What is single inheritance in Python?
  4. What is multilevel inheritance in Python?
  5. What is multiple inheritance in Python?
  6. What is hierarchical inheritance in Python?
  7. What is hybrid inheritance in Python?
Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts
Total
0
Share