Skip to content
Advertisement

How to call the method of the subclass using method of Parent class?

I have a Shape method that has two arguments first is the width and the second is the height. I have two subclasses, one is the rectangle and another is triangle. I want to call the area() the method defined in both triangle and rectangle with the help of the area() of the Shape class. I have written this piece of code but when I am calling the area() method of derived classes using the area() method of the parent class, I am getting an error. So how to do this?

JavaScript

Advertisement

Answer

You want to override the method and let the subclasses implement it. You do not need to call any method from Shape.area() at all!

JavaScript

With that in place, you can do:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement