Hierarchical Inheritance

in CCSyesterday

Hello everyone!

Today I am here to share Hierarchical Inheritance in Object Oriented Programming (OOP) in dart.

image.png

In hierarchical inheritance the parent class is shared by multiple child classes. The method inside the parent class is overridden by the child classes.


image.png

This is the parent class Device{}. I have defined a method poweron() inside the parent class. I have set an example print statement in the method. Now I will create some child classes which will override this method and they will show their own unique poweron message.


image.png

I have created this child class Mobile{}. In this class which extends the parent class Device I have overridden the method poweron(). I have displayed a different statement here by overriding the method from the parent class.


image.png

Similarly I have created this child class Laptop{}. In this child class I have overridden the poweron() method again and inside the method I have changed the statement accordingly.


image.png

Now in the main I have created the objects of both the child classes. Then I have called poweron() method from both the child classes. Each object has printed specific message with respect to the overridden method.


image.png

Here you can see the output of the program. Actually here in this program each child class inherited the parent class and then overridden the method of the parent class. This is how hierarchical inheritance works.

Posted using SteemX

Sort:  

🎉 Congratulations!

Your post has been upvoted by the SteemX Team! 🚀

SteemX is a modern, user-friendly and powerful platform built for the Steem community.

🔗 Visit us: www.steemx.org

✅ Support our work — Vote for our witness: bountyking5

banner.jpg