Skip to content
Advertisement

Pass calculated values by inherited classes to main class

So in my main function I got a class named Figure

Circle and Rectangle extend class Figure. Square extends class Rectangle.

Now I have to print the values from created objects. I think I am approaching this in wrong way. How can I pass the values to the print function that have been created by other classes?

JavaScript
JavaScript

Should I not be using return in the calcPerimeter and calcArea methods? Because I need to pass this somehow to Figure class. Also I have same as above class for rectangle.

And this class that extends rectangle.

JavaScript

Can someone advice me how to do it properly, I just started my journey with Java and Im practicing on objects.

Advertisement

Answer

Firstly, you have made all 3 members of Figure class as private, so if you want the base classes to update them, then make them protected, and use super.area, super.perimeter, etc. to update them.

Yes you dont need to return but update parent’s values using the super keyword.

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