You are currently viewing Inheritance In Python – Single, Multiple, Multi-level Inheritance And More

Inheritance In Python – Single, Multiple, Multi-level Inheritance And More

We all have ever encountered the term OOPs (Object-Oriented Programming) in programming. OOP is an integral part of any programming language that helps carry out complex concepts. It provides a way to structure and organize the code in such a manner that makes it easier to maintain and scale the code.

Classes are an essential concept in OOP or we can say that classes are the building blocks of Object-Oriented Programming which is a programming model based on objects and classes.

Object-Oriented Programming has four important concepts and one of them is inheritance.

What is Inheritance?

Inheritance can be defined as the mechanism that permits the newly created classes to inherit the methods and attributes of the existing class or parent class. The classes that inherit the methods and attributes from the parent class are called subclass and the existing class is called a superclass.

As soon as a subclass inherits the superclass, it gains all of the methods and attributes of the superclass. This allows us to reuse the code as well as we can extend or modify the behavior of the superclass.

For example-

The class Hero has a method power that returns a specific power a hero has. Then we created a subclass Ironman that inherits the superclass Hero and the subclass Ironman has its own method speciality that specifies the specialties of Ironman.

The subclass Ironman now has access to all the attributes and methods of the superclass Hero as well.

We accessed the method power using the class Ironman which inherited the method power from the class Hero.

Inheritance helps us reduce the effort that we put into writing the same logic again and again and makes it easy to maintain and update the code.

Different types of inheritance:

  • Single Inheritance
  • Multiple Inheritance
  • Multilevel Inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance

Single Inheritance

Single Inheritance

Single inheritance can be defined as an inheritance where a subclass or derived class inherits from a single superclass or parent class.

In simple words, the derived class or subclass has only one direct parent class. Here is an example of single inheritance.

The above code is no different from the code that we discussed earlier where we saw the first glimpse of class inheritance in Python.

The subclass Number inherited the superclass Calculate and now the methods inside it can now be accessed by the subclass Number. We created the instance for the subclass Number and then accessed the sum function from the superclass or parent class Calculate.

Using constructor

Until now, we saw a simple demonstration of the class inheritance in which a subclass gains the methods and attributes of the superclass by just passing it as an argument.

Now we’ll see how to perform inheritance when a class has a constructor function.

In the above code, we created the base class Hero and inside it, we defined the constructor function that takes two arguments reel_name and hero_name and then we defined the function show that prints the reel_name and hero_name.

Then we created a subclass Name that inherits the superclass Hero and then we created a constructor function that takes three arguments real_namereel_name, and hero_name.

In order to access the objects of the parent class Hero, we invoked __init__ of the parent class Hero and passed the required arguments.

Next, we created a function display that prints the real_name and calls the function show from the class Hero.

Then we created obj an instance of the class Name and called the function display and show.

What if we didn’t call the __init__ of the parent class?

We have done everything similar to what we did earlier but this time we didn’t call the __init__ of the parent class Cal and tried to call the function sq from it.

We encountered an error and this happened because the objects of the parent class Cal were not available for the subclass or child class X.

Multiple Inheritance

Multiple Inheritance

Python supports multiple-class inheritance and can be defined as an inheritance where a subclass or child class inherits from more than one superclass.

In short, a subclass has more than one direct parent class or superclass. Let’s understand the concept of multiple-class inheritance with an example.

In the above code, subclass Child inherited two superclasses named First and Second that will help gain the attributes of both superclasses inside the subclass Child.

We then created a function combine that prints the values of self.greet and self.name and a string. We created obj which is an instance of the class and then called the combine function.

Note: There is a risk of using multiple inheritances because it can lead to uncertainty in the code and also cause a “Diamond Problem” where a subclass inherits certain conflicting methods from multiple parent classes.

Diamond Problem

The “Diamond Problem” often occurs primarily in the case of multiple inheritance where a subclass inherits the conflicting methods from the multiple superclasses that eventually create ambiguity in the code.

In the above code, subclasses Geek and Python inherit from the superclass Hero and then another subclass GeekPython inherits from the subclasses Geek and Python. They all have a function with the same name func.

Now when we call the func, the order of the parent class decides from which class GeekPython inherits from. So, in this case, the function func inside the class Geek will get executed.

If we change the order of the class, then we’ll get the following output.

Multi-level Inheritance

Multilevel Inheritance

Multi-level inheritance can be defined as where a subclass inherits from the single subclass and then another subclass inherits from the first subclass. By this, the second subclass can access all the attributes and methods from both the first subclass and the superclass.

Let’s understand with an example showing multi-level inheritance.

In the above code, the subclass Parent inherits from the superclass GrandPa and now the first subclass Parent has access to the methods of the superclass GrandPa.

Then another subclass GrandChild inherited from the first subclass Parent and it has access to the methods that both the first subclass and superclass have.

Hierarchical Inheritance

Hierarchical Inheritance

Hierarchical inheritance can be defined as when more than one derived class inherits from a single base class. Let’s understand with an example.

In the above code, both derived class Add and Subtract inherited from the superclass or base class Calculate.

Hybrid Inheritance

Hybrid Inheritance

This type of inheritance is a blend of different inheritance which means it has a combination of two different types of inheritance like multiple or multi-level inheritances or multiple or single inheritances.

Here is an example showing the demonstration of hybrid inheritance.

In the above code, the subclasses Luffy and Naruto inherit from the superclass Role and then the subclass Anime inherits from the subclasses Luffy and Naruto.

The first scenario where Luffy and Naruto inherit from the superclass Role shows the single inheritance and the second scenario where Anime inherits from the subclasses Luffy and Naruto shows the multiple inheritances.

Conclusion

Inheritance is one of the pillars of Object-Oriented Programming (OOP) that allows a new class to inherit the methods and attributes of an existing class or parent class.

Python is one of the programming languages that support multiple inheritance of the class but it could lead to uncertainty in the code.

Let’s review what we’ve learned:

We saw the concepts of different types of inheritance in Python:

  • Single Inheritance
  • Multiple Inheritance
  • Multi-level Inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance

🏆Other articles you might like if you like this article

Public, Protected and Private access modifiers in Python.

Build your own API in Python using FastAPI in a few steps.

A modern way to perform string interpolation in Python.

Build your own CLI in Python using the argparse module.

Different ways to remove whitespaces from the string in Python.

Bitwise operators explained with examples in Python.


That’s all for now

Keep Coding✌✌