What is Multiple Inheritance in Python?

What is Multiple Inheritance in Python?

Are you looking to learn about multiple inheritances in Python? If yes, stick around this article to explore more about inheritance in Python.

Python has provided us with different types of inheritance, including single, multiple, multilevel, hybrid, and hierarchical inheritance. We can use these according to the nature of the software product we aim to develop, as these types of inheritance are use-case-dependent. To learn about the different types of inheritance that can be implemented with Python, you can refer to our more comprehensive guide here.

This article focuses on multiple inheritances, an advanced version of inheritance in Python that allows you to inherit more than one parent/base class into a child/derived class 🤔. Yes, you heard it right; you can access the properties of more than one base class into a single derived class. 

Let’s dive deep into it!

 

How Does Multiple Inheritance in Python Work?

As discussed, we’ll inherit more than one base class into a single derived class. It is a core concept of object-oriented programming that allows you the inherit the characteristics of more than one parent class into a single-child class. 

Multiple inheritance in Python

It improves the code reusability factor because you can inherit the pre-defined properties and functions from the base classes.

 

Syntax in Python:

class BaseClass1():

#Body of BaseClass1

class BaseClass2():

#Body of BaseClass2

class DerivedClass(BaseClass1, BaseClass2):

#Body of DerivedClass

 

To understand this more, let’s see a practical example:

 

Code

# base class 1

class Father():

    def father(self):

        print("Hi! Son i'm your daddy")

    

# base class 2

class Mother(BaseClass):

    def mother(self):

        print("Hey! Dear I'm you mommy")

        

# derived class      

class Son(Father, Mother):

    def son(self):

        print("I love you Mommy n Daddy")

        

# creating object of derived class

son = Son()




# calling functions with the derived class object

son.father()

son.mother()

son.son()

 

Output

Hi! Son i'm your daddy

Hey! Dear I'm you mommy

I love you Mommy n Daddy

 

As you can see, the class Son inherits both classes, Mother and Father. This example is similar to a real-life family relationship scenario, where a son or a daughter is authorized to access the property of their parents.

 

 

Conclusion

To conclude the article on multiple inheritance in Python, we have discussed what it is and how it works, along with a practical example. As discussed, each type is suitable for specific scenarios, and you can use these according to the nature of your software product. So choose wisely and get the job done efficiently. 🙂

Brainstorming moment, name any suitable scenarios where we use multiple inheritance efficiently.

Total
0
Shares
Leave a Reply

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

Related Posts
Total
0
Share