Search code examples
javainheritanceencapsulationgetter-settersuperclass

How to get the field of an object, instantiated in the superclass, from a subclass of a subclass


I need to get a field of an object instantiated in a superclass. The problem is that I need to get it from a subclass two levels deep from the superclass, that is, I am in the class SingleChart which extends SingleTable, which itself extendsTemplateReport class. TemplateReport instantiates the private Report object. Report has public getters and setters. I want to retrieve the height field of Report. Is there a way of doing this directly from SingleChart to TemplateReport?


Solution

  • Private instance variables are inherited by the sub-classes.

    So if TemplateReport makes a Report object, and Report has getters and setters. You are able to get the Report object from any subclass by call the getter and then asking for the value you need.