Is the term hybrid inheritance in Python new to you 🙄? Spend the next five minutes with me to understand the concept of this type of inheritance.
In Python, we have different types of inheritance, including single, multilevel, multiple, hierarchical, and hybrid. To learn about the different types of inheritance that can be implemented with Python, you can refer to our more comprehensive guide here.
In this article, we’ll discuss hybrid inheritance and how it works in Python, along with a practical example or implementation, so let’s dive deep into the topic and understand it.
What is Hybrid Inheritance in Python?
Hybrid inheritance is an inheritance of two or more different types, like the combination of single and hierarchical inheritance and vice versa. Sometimes there are scenarios of complex software problems that require different types of inheritance combinations to provide a suitable solution.
Let’s take an example of a family hierarchy: a grandfather has one son, and he has one daughter and two sons. So is it confusing 🤯 look at the below diagram to understand it more clearly:
Now let’s convert the above scenario into a practical Python example. To do so, we’ll need two types of inheritance single and hierarchical:
Code
# base class class GrandFather(): def grandFather(self): print("I'm Baron the father of your father") # derived class for GrandFather, and Base class for the derived SonTom, SonBob, and DaughterJenny class Father(GrandFather): def father(self): print("I'm John your father") # derived class class SonTom(Father): def tom(self): print("Hey! I'm Tom") # derived class class SonBob(Father): def bob(self): print("Hey! I'm Bob") # derived class class DaughterJenny(Father): def jenny(self): print("Hey! I'm Jenny") # creating objects of derived classes tom = SonTom() bob = SonBob() jenny = DaughterJenny() print("Tom") tom.tom() tom.father() tom.grandFather() print("\nBob") bob.bob() bob.father() bob.grandFather() print("\nJenny") jenny.jenny() jenny.father() jenny.grandFather()
Output
Tom Hey! I'm Tom I'm John your father I'm Baron the father of your father Bob Hey! I'm Bob I'm John your father I'm Baron the father of your father Jenny Hey! I'm Jenny I'm John your father I'm Baron the father of your father
As you can see in the above example, we have a base (parent) class GrandFather and a derived (child) class Father which is single inheritance. Furthermore, there are three more derived (child) classes SonTom, SonBob, and DaughterJenny, inherited from the Father class.
Now, in this case, Father is a derived class for GrandFather and a base class for SonTom, SonBob, and DaughterJenny classes, so Father is both a base and a derived class.
In the above example class, Father is working as a bridge, allowing the derived classes to access the functions and properties of the class Father and the class GrandFather with the help of their objects.
Conclusion
To conclude this article on hybrid inheritance in Python, we have discussed what it is and how it works, along with a real-life example. Hybrid inheritance combines two or more different types of inheritances used to solve a complex software problem.
There aren’t any hard and fast rules; you can build any custom inheritance structure accordingly.
Finally, you considerably understand hybrid inheritance; it’s time to recall the concepts—comment below a scenario suitable to showcase this, along with the types of inheritance used in it.