r123454321
r123454321

Reputation: 3403

Accessing parent class instance variables from child class instances

So right now, I have a Preprocessor class that generates a bunch of instance variable maps, and a Service class that has a setPreprocessor(Preprocessor x) method, so that an instance of the Service class is able to access the maps that the preprocessor generated.

At the moment, my Service class needs to call three methods in succession; for sake of simplicity, let's call them executePhaseOne, executePhaseTwo, and executePhaseThree. Each of these three methods instantiate/modify Service instance variables, some of which are pointers to the Service instance's Preprocessor object.

My code has this 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 my code, I want to put each executePhaseXXX() call in its own separate subclass of Service, and leave the common data structures for all the phases in the parent class Service. Then, I want to have an execute() method in the Service parent class that executes all three phases in succession:

class ServiceChildOne extends Service {
    public void executePhaseOne() {
        // Do stuff
    }
}

class ServiceChildTwo extends Service {
    public void executePhaseTwo() {
        // Do stuff
    }
}

class ServiceChildThree extends Service {
    public void executePhaseThree() {
        // Do stuff
    }
}

EDIT:

The problem is, how do I write my execute() method in the Service parent class? I 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, my childOne, childTwo, and childThree objects aren't able to access the preprocessor instance variable that lives in the parent class Service... How could I get past this problem?

Upvotes: 1

Views: 24116

Answers (4)

Steve
Steve

Reputation: 716

It looks like your problem is that you have not one, but four different instances of Service - each of which has its own uninitialized copy of the base class variables.

The only solutions I can think of at the moment are, first, the rather poor design of making your Service member variables static - which, in effect, means you can only have one version of Service at a time. A better solution, to my mind, would be to not make the processing phases subclasses, but instead make them independent classes that take an instance of Service as a parameter.

EDIT: For a quick example, the Service class could look like:

class Service
{
    public Service() { ... }
    public Preprocessor getPreprocessor() { ... }
    public void setPreprocessor(Preprocessor preprocessor { ... }
    public Type2 getVariable2() { ... }
    public void setVariable2(Type2 variable2) { ... }
        ...
}

and the phase classes could look something like:

class ServicePhaseOne
{
    private Service m_dataHost;

    public ServicePhaseOne(Service dataHost)
    {
        m_dataHost = dataHost;
    }

    public void executePhaseOne()
    {
        // Do phase 1 stuff
    }
}

... and so on for phase 2 and phase 3.

The execute() method would then look like:

public void execute()
{
    ServicePhaseOne phaseOne = new ServicePhaseOne(this);
    ServicePhaseTwo phaseTwo = new ServicePhaseTwo(this);
    ServicePhaseThree phaseThree = new ServicePhaseThree(this);
    phaseOne .executePhaseOne();
    phaseTwo .executePhaseTwo();
    phaseThree .executePhaseThree();
}

Upvotes: 0

Alex
Alex

Reputation: 11579

Your preprocessor should be protected or public to be able to have an access from the child. You can read about modifiers here.

UPDATE

new ServiceChildOne(new Preprocessor());
.....

class ServiceChildOne extends Service {

    public ServiceChildOne(Preprocessor preprocessor) {
       super.preprocessor = preprocessor;
    }

    public void executePhaseOne() {
        // Do stuff
    }
}

Upvotes: 0

Vikram
Vikram

Reputation: 4190

you could provide some method like getPreprocessorInstance() in your Service class that returns Preprocessor instance

Upvotes: 0

superEb
superEb

Reputation: 5673

Use the protected modifier for your Preprocessor instance variable of Service, like so:

public class Service {
    protected Preprocessor preprocessor;
}

Then each subclass of Service has a this.preprocessor.

Upvotes: 3

Related Questions