Day #80 - Multilevel Inheritance in Python

Day #80 - Multilevel Inheritance in Python

Introduction

Welcome to my 80th blog post on the Python journey. On day 80, I learned Multilevel inheritance which allows us to inherit attributes and methods of a derived class into another sub-derived class that is a child of the derived class. Let's dive into more details and understand the working of multilevel inheritance in Python.

So let's get started......

Multilevel Inheritance

  • In this type of inheritance, a derived class inherits from another derived class.

  • This type of inheritance allows us to build a hierarchy of classes where one class builds upon another, creating a more specialized class.

  • In Python, multilevel inheritance is achieved by using the class hierarchy.

Syntax

In the below code, we have three classes: BaseClass, DerivedClass1, and DerivedClass2. The DerivedClass1 class inherits from the BaseClass, and the DerivedClass2 class inherits from the DerivedClass1 class.

The above structure creates a hierarchy where DerivedClass2 has access to all the attributes and methods of both the parent and grandparent class i.e DerivedClass1 and BaseClass.

class BaseClass:
    # Base class code

class DerivedClass1(BaseClass):
    # Derived class 1 code

class DerivedClass2(DerivedClass1):
    # Derived class 2 code

Example

In this example, we have three classes: Animal, Dog, and GoldenRetriever. The Dog class inherits from the Animal class, and the GoldenRetriever class inherits from the Dog class.

Now, when we create an object of the GoldenRetriever class, it has access to all the attributes and methods of the Animal class and the Dog class. We can also see that the GoldenRetriever class has its own attributes and methods that are specific to the class.

class Animal:
    def __init__(self, name, species):
        self.name = name
        self.species = species

    def show_details(self):
        print(f"Name: {self.name}")
        print(f"Species: {self.species}")

class Dog(Animal):
    def __init__(self, name, breed):
        Animal.__init__(self, name, species="Dog")
        self.breed = breed

    def show_details(self):
        Animal.show_details(self)
        print(f"Breed: {self.breed}")

class GoldenRetriever(Dog):
    def __init__(self, name, color):
        Dog.__init__(self, name, breed="Golden Retriever")
        self.color = color

    def show_details(self):
        Dog.show_details(self)
        print(f"Color: {self.color}")

dog = GoldenRetriever("Max", "Golden")
dog.show_details()
Name: Max
Species: Dog
Breed: Golden Retriever
Color: Golden

Features of Multilevel Inheritance

  • It allows us to create more complex and intricate classes by building upon existing ones.

  • It provides provision for code reusability. This helps to avoid writing the same set of code multiple times.

  • This leads to better readability and easy maintenance of code

Resources Used

You can watch the video of Day#80 by clicking on the below link 👇👇👇👇👇

Conclusion

Thanks, guys for going through this blog post. On day 80, I learned about the multilevel inheritance that allows us to create complex and intricate classes by building upon existing ones. This increases code reusability, readability, and code maintenance.

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 #80

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.