Introduction
Welcome to my 81st blog post on the Python journey. On day 81, I learned about Hybrid and Hierarchical Inheritance in Python. Let's dive into more details and understand these concepts in Python with practical examples.
So let's get started......
Hybrid Inheritance
It's a combination of multiple inheritance and single inheritance in object-oriented programming.
In this case, multiple inheritance is used to inherit the properties of multiple base classes into a single derived class, and single inheritance is used to inherit the properties of the derived class into a sub-derived class.
Hybrid inheritance can be implemented by creating a class hierarchy.
Syntax
# Example of Hybrid Inheritance
class BaseClass:
pass # attributes and methods
class Derived1(BaseClass):
pass # attributes and methods
class Derived2(BaseClass):
pass # attributes and methods
class Derived3(Derived1, Derived2):
pass # attributes and methods
In the above code, it can be seen that Derived1 and Derived2 classes are single inheritance and Derived3 class follows a multiple inheritance inheriting properties and attributes from Derived1 and Derived2 Classes
Example - Hybrid Inheritance
In the below Example, the Student class inherits from the Person class and the Person class inherits from the Human class. The Student class also has a Program class that it is associated with.
class Human:
def __init__(self, name, age):
self.name = name
self.age = age
def show_details(self):
print("Name:", self.name)
print("Age:", self.age)
class Person(Human):
def __init__(self, name, age, address):
Human.__init__(self, name, age)
self.address = address
def show_details(self):
Human.show_details(self)
print("Address:", self.address)
class Program:
def __init__(self, program_name, duration):
self.program_name = program_name
self.duration = duration
def show_details(self):
print("Program Name:", self.program_name)
print("Duration:", self.duration)
class Student(Person):
def __init__(self, name, age, address, program):
Person.__init__(self, name, age, address)
self.program = program
def show_details(self):
Person.show_details(self)
self.program.show_details()
program = Program("Computer Science", 4)
student = Student("John Doe", 25, "123 Main St.", program)
student.show_details()
Output -
Name: John Doe
Age: 25
Address: 123 Main St.
Program Name: Computer Science
Duration: 4
From the above code we can see that the Student
object has access to all the attributes and methods of the Person
and Human
classes, as well as access to the Program
class through association.
In this way, hybrid inheritance allows for a flexible and powerful way to inherit attributes and behaviors from multiple classes in a hierarchy or chain.
Hierarchical Inheritance
Here multiple subclasses inherit from a single base class.
In other words, a single base class acts as a parent class for multiple subclasses. This helps in building relationships between classes in a hierarchical manner.
Syntax
# Hierarchical Inheritance
class BaseClass:
pass
class D1(BaseClass):
pass
class D2(BaseClass):
pass
class D3(D1):
pass
Example - Hierarchical Inheritance
In the below example, the Animal class acts as the base class for two subclasses, Dog and Cat that inherit the attributes and methods of the Animal class.
class Animal:
def __init__(self, name):
self.name = name
def show_details(self):
print("Name:", self.name)
class Dog(Animal):
def __init__(self, name, breed):
Animal.__init__(self, name)
self.breed = breed
def show_details(self):
Animal.show_details(self)
print("Species: Dog")
print("Breed:", self.breed)
class Cat(Animal):
def __init__(self, name, color):
Animal.__init__(self, name)
self.color = color
def show_details(self):
Animal.show_details(self)
print("Species: Cat")
print("Color:", self.color)
dog = Dog("Max", "Golden Retriever")
dog.show_details()
cat = Cat("Luna", "Black")
cat.show_details()
Output
Name: Max
Species: Dog
Breed: Golden Retriever
Name: Luna
Species: Cat
Color: Black
So in hierarchical inheritance relationships are established between classes in a hierarchical manner. This allows multiple subclasses to inherit from a single base class. This in turn helps in code reuse and organization of code in a more structured manner.
Resources Used
You can watch the video of Day#81 by clicking on the below link 👇👇👇👇👇
Conclusion
Thanks, guys for going through this blog post. On day 81, I learned about Hybrid and Hierarchical Inheritance that further assist in code reusability. I learned about these concepts through practical examples and wrote a basic program on these OOP's concepts.
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 #81
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.