Assuming I have the abstract Shape class, within I have the method getArea()
. As a subclass of the Shape class, I have the Rectangle class, which overrides the getArea()
method. As a subclass of the Rectangle class, I have the Square class, but in the Square class, I want to reference the getArea()
as implemented in the Shape class. How can I do this without copying the code over?
Asked
Active
Viewed 39 times
0
-
1You can access the immediate superclass' method with `super.getArea()`, but you can't jump in the hierarchy at will. That indicates design problems. – Kayaman Sep 25 '20 at 18:20
-
1You just override the getArea() method. I do not believe that you can get to the getArea() of a "grandparent" object. I think if you want to, then you might be having an incorrect class hierarchy. I would probably have Square be a special case of Rectangle rather than a child of it. – NomadMaker Sep 25 '20 at 18:20
-
1Polymorphism doesn't allbow this. This is already answered [here](https://stackoverflow.com/questions/3118067/java-how-do-you-access-a-parent-class-method-two-levels-down) and [here](https://stackoverflow.com/questions/2584377/java-how-to-call-method-of-grand-parents). – Kevin Seymour Sep 25 '20 at 18:21
-
1Does this answer your question? [Java How to call method of grand parents?](https://stackoverflow.com/questions/2584377/java-how-to-call-method-of-grand-parents) – Kevin Seymour Sep 25 '20 at 18:21