Introduction
Welcome to my 79th blog post on the Python journey. On day 79, I learned about the next type of inheritance which is Multiple inheritance which allows a class to inherit attributes and methods from multiple parent classes. Let's dive into more details and understand Multiple inheritances in Python.
So let's get started......
Multiple Inheritance
It allows a class to inherit attributes and methods from multiple parent classes.
It is helpful in situations where a class needs to inherit functionality from multiple sources.
Syntax -
To implement multiple inheritance specify the parent class in the child's class definition and separate it by (,) comma's.
In the below example, the ChildClass
inherits attributes and methods from all three parent classes: ParentClass1
, ParentClass2
, and ParentClass3
.
Check out below syntax
class ChildClass(ParentClass1, ParentClass2, ParentClass3):
# class body
METHOD RESOLUTION ORDER
Note : In case of multiple inheritance, Python follows a method resolution order (MRO) to resolve conflicts between methods or attributes from different parent classes.
The MRO helps in determining the order in which parent classes are searched for attributes and methods when multiple parent classes are passed in the child class definition.
Example - Multiple Inheritance
In the below example, the Dog class inherits from both the Animal and the mammal classes, so it can use attributes and methods from both parent classes.
class Animal:
def __init__(self, name, species):
self.name = name
self.species = species
def make_sound(self):
print("Sound made by the animal")
class Mammal:
def __init__(self, name, fur_color):
self.name = name
self.fur_color = fur_color
class Dog(Animal, Mammal):
def __init__(self, name, breed, fur_color):
Animal.__init__(self, name, species="Dog")
Mammal.__init__(self, name, fur_color)
self.breed = breed
def make_sound(self):
print("Bark!")
Another Example -
In the below example, the DancerEmployee class inherits from the multiple parent classes Employee and Dancer.
class Employee:
def __init__(self, name):
self.name = name
def show(self):
print(f"The name is {self.name}")
class Dancer:
def __init__(self, dance):
self.dance = dance
def show(self):
print(f"The dance is {self.dance}")
class DancerEmployee(Employee, Dancer):
def __init__(self, dance, name):
self.dance = dance
self.name = name
o = DancerEmployee("Kathak", "Shivani")
print(o.name)
print(o.dance)
o.show()
print(DancerEmployee.mro())
Output -
Shivani
Kathak
The name is Shivani
[<class '__main__.DancerEmployee'>, <class '__main__.Employee'>, <class '__main__.Dancer'>, <class 'object'>]
Resources Used
You can watch the video of Day#79 by clicking on the below link 👇👇👇👇👇
Conclusion
Thanks, guys for going through this blog post. On day 79, I learned about another powerful Python feature i.e Multiple inheritance that allows us to inherit methods and attributes from multiple parent classes. I also wrote a bunch of code and explained by the mentor to practice Multiple inheritance.
Thank you if you read this post and have found this post useful. I hope you have joined me and are enjoying my magical journey of Python coding. This is it for Day #79
See you in the next one.....
About Me
Hey Guys, I am Chintan Jain from CodeWithJain. I am a trader and content creator. I am also passionate about tech and hence wanted to explore the field of tech. I always wanted to learn to code so I watched many tutorials but procrastinated practicing coding. To get into the habit of coding consistently I am starting to BLOG with HASHNODE on daily basis.
I will document my coding journey from scratch and share my daily learnings in a blog post on HASHNODE. I hope you all will enjoy my content and my coding journey.
So what are you waiting for, smash the FOLLOW and LIKE buttons and follow along my coding journey, a step to create more beautiful digital products and empower people.