Using superclass fields in subclass method
Hi, I have a question about inheritance. I'm doing an assignment after a chapter on inheritance, and wanted to get something straight before I continue.
The question I'm currently doing is asking me to do the following:
-Create a Square class with private height and width fields, and a computeSurfaceArea method.
-Create a Cube class derived from the Square class with an added (private) depth field, and with an overriding computeSurfaceArea method.
Now, ignoring the fact that a square should have sides of equal length, etc, here's what I'm wondering:
In the Cube subclass's computeSurfaceArea method, I want to use the height and width that are inherited from the Square class along with my new depth field. The only way I can currently see to do this is to declare two new variables whose values come from the Square's getHeight() and getWidth() methods. This is the only way I can get the values of width and height. However, this, to me, seems to pretty much defeat the purpose of inheriting the fields from the parent class in the first place.
I'm basically just wondering if there's a more efficient way to do this without having the Square fields declared as protected instead of private. Thanks,