3

I have 3 subclasses (LowState, MediumState, HighState) which derive from the same superclass (State).

Within the superclass, State, I have declared several variables which need to be accessed and changed from the 3 subclasses.

Is it possible to instantiate these 3 subclass objects from the same superclass object so that they can all access the variables from the same object?

Alternatively, I can just declare the variables in each subclass and then pass the data between the objects. However, I'm concerned that this chaining method could get out of hand.

  • 4
    For the shared state, what you really want is composition, not inheritance, where the current children take the base class (which won't really be base class anymore) as their property during construction and the base class has accessors to give you access to the shared attributes. Then during object graph constructions you just need to make sure you inject the same instance of the base class into all three children, so when one children modifies its property, the change will reflect onto the remaining classes as well. – Andy May 20 '16 at 11:42
  • Unfortunately inheritance is a must as I'm using virtual methods in all 3 subclasses. – M-R May 20 '16 at 12:00
  • 1
    You can still make the three derived states to be children of some base class, but the shared state should be represented by another class, not the parent of the children, and same instance of the another class should be, as I have said in the first comment, injected into all instances of the children. – Andy May 20 '16 at 12:07
  • @DavidPacker, Ahhhh so you're suggesting storing those shared variables in their own object which is passed to the subclass objects? – M-R May 20 '16 at 14:44
  • @DavidPacker, would that be more sensible than just passing each individual variable? As there aren't many, it seems overkill to have a class nothing more than get and set methods. – M-R May 20 '16 at 14:45
0

Is it possible to instantiate these 3 subclass objects from the same "superclass object" so that they can all access the variables from the SAME object?

No. But you can instantiate from a State that has its members defaulted to particular values, and consequently your subclasses will all have the same values.

public class State {
   protected int stateValue = 1;
}

public class SubState extends State {
   // stateValue will be '1' here...
}

If you used 'composition' rather than inheritance, you could instantiate your 'SubState' objects referencing the same 'State' object. However you'd have to be wary of sharing that object across multiple subclassed object, and that doesn't seem very safe or intuitive.

Noting your comment below, composition would provide that means of sharing state change e.g.

public class SubState(State state) {
   // 'state' could be shared across multiple substates here...
}

and you don't need the superclass/subclass relationship that you're referring to.

  • But if one of the subclass states changed the value of stateValue, surely the others wouldn't see the update. – M-R May 20 '16 at 11:17
  • See my edit above... – Brian Agnew May 20 '16 at 11:20
  • Hmmmm do you think it would just be safer to pass the values between objects? – M-R May 20 '16 at 11:21
  • I would suggest it's perhaps more intuitive – Brian Agnew May 20 '16 at 11:22
2

Within the superclass, State, I have declared several variables which need to be accessed and changed from the 3 subclasses.

That's what the protected modifier is for;

"# Me and my Subclasses ...".

Is it possible to instantiate these 3 subclass objects from the same "superclass object" so that they can all access the variables ...

That you get from the protected modifier.

... from the SAME object?

Now that sounds like a strange (and dangerous) thing to want to do.

Why would you want to change these values? You're potentially getting into all kinds of issues to manage changes from the different subclasses.

If you really want to do this, then the static modifier might get you want you want, but may well come with a heap of other, "new and exciting" problems to deal with (especially in unit testing/ mocking).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.