java - Accessing parent class instance variables from child class instances -
so right now, have preprocessor class generates bunch of instance variable maps, , service class has setpreprocessor(preprocessor x) method, instance of service class able access maps preprocessor generated.
at moment, service class needs call 3 methods in succession; sake of simplicity, let's call them executephaseone, executephasetwo, , executephasethree. each of these 3 methods instantiate/modify service instance variables, of pointers service instance's preprocessor object.
my code has structure right now:
preprocessor preprocessor = new preprocessor(); preprocessor.preprocess(); service service = new service(); service.setpreprocessor(preprocessor); service.executephaseone(); service.executephasetwo(); service.executephasethree(); to better organize code, want put each executephasexxx() call in own separate subclass of service, , leave common data structures phases in parent class service. then, want have execute() method in service parent class executes 3 phases in succession:
class servicechildone extends service { public void executephaseone() { // stuff } } class servicechildtwo extends service { public void executephasetwo() { // stuff } } class servicechildthree extends service { public void executephasethree() { // stuff } } edit:
the problem is, how write execute() method in service parent class? have:
public void execute() { servicechildone childone = new servicechildone(); servicechildtwo childtwo = new servicechildtwo(); servicechildthree childthree = new servicechildthree(); system.out.println(childone.preprocessor); // prints null childone.executephaseone(); childone.executephasetwo(); childone.executephasethree(); } however, childone, childtwo, , childthree objects aren't able access preprocessor instance variable lives in parent class service... how past problem?
use protected modifier preprocessor instance variable of service, so:
public class service { protected preprocessor preprocessor; } then each subclass of service has this.preprocessor.
Comments
Post a Comment