DEV Community

Cover image for The Power of Inheritance
Snehangsu De
Snehangsu De

Posted on • Originally published at xspace.hashnode.dev

The Power of Inheritance

If you Google "Inheritance" the most expected definition in the language of Biology would be that it's a trait passed down from your ancestors to you or from you to your descendants.

Me, you, alike have had some specific traits or qualities that have been passed down from our ancestors. While true, this doesn't mean I or you are exactly the same as our ancestors i.e. we have our own qualities and traits in addition to the traits from them.

Inheritance in Machines

Inheritance is similar in the life of machines. It helps them take in a specific trait or functionality from a Parent Class but also add a few functionalities of its own. If you are to ask me, Inheritance is undoubtedly one of the most powerful features in Object-Oriented Programming.

It allows us to create an entirely new Class with no modification or changes to the Parent Class. The new class is called the Child Class and the class it inherits from is the Parent Class.

In the image, we have two Classes Animal, which is the Parent Class, and Dog, which is the Child Class. We have talked about how to define classes before and that stays exactly the same for the Parent Class. However, we have two new things which are on Line 9 and Line 11. Let's dive in.

Line 9: To define a Child Class Dog, which would inherit all the functionality from the Parent class Animal we declare the Parent Class name within parenthesis.

Line 11: Now that we have defined the Parent Class we want to inherit from we would need to specify an in-built function that allows us to inherit the functionality to the Child Class. super().__init__() does the exact same thing.

We will see how this works as we walk through an example below

Everything else stays the same. The Parent Class has a method breathe while the Child Class has its own method bark. Also, the Child Class takes in one positional argument which is the breed of the Dog.

Let's now look at how to use these in our code.

The initial surprise that came to me when I was learning Inheritance was that I didn't really need the Parent Class to work with the Child Class, which is such a beautiful thing.

Line 1: We have defined two Classes before Animal and Dog. Surprise, surprise, we don't need the Animal Class to work with our Dog Class though it actually inherits from the Animal Class. Less Code, More Functionality.

Line 3: We have created an object roxy with our class Dog passing in one positional argument 'Border Collie'.

Line 5–8: These are the attributes of the Child Class i.e. Dog. Tapping into the methods and attributes provides the same output that we defined inside the Class.

Line 11–14: These are the attributes of the Parent Class i.e. Animal. This is the interesting part, Dog being a Child Class inherits all the attributes of the Parent Class without being explicitly defined. This is the work of the super() function which has the ability to reference the Parent Class.

Without the super() function trying to work with Line 11 -14 would result in an Attribute Error as it would not be inheriting any attribute of Parent Class. However, the method breathe() will still be inherited.

Multiple Inheritance

Inheritance can go into depth as well, just like us humans can inherit genes from our great grandparents. However, the important thing to remember is that the entire understanding of Inheritance stays the same i.e. it inherits functionality from the Parent Class.

In the above image, I haven't written the demo code for the classes but the structure of the Class is shown.

Conclusion

We have now embraced the power of Inheritance and why it makes our life even easier. All of this while reducing redundant lines of code and making our code re-useable. I hope you now have a good grasp of Inheritance. I look forward to seeing more of this functionality used in projects, do comment below if you have any interesting projects or in general comments or feedback.

I hope this message finds you in good health! If you have any interesting suggestions or feedback, feel free to connect with me on Twitter.

Top comments (0)