What you can do is give the subclass a variable that is called exactly the same, and then set it equal to the superclass using constructors and methods like
public abstract class Command{ private Mediator m public Command(Mediator med){ m = med; } abstract void exec(); } public class FoobarCommand extends Command{ private Mediator m; public FoobarCommand(Mediator med){ super(med); m = med; } public void exec(){ m.doAFoobar() } } public static void main(String[] args){ Mediator m = new Mediator(); Command c = new FoobarCommand(m); c.exec(); }
However, this is limited to what he can do. Since m
is an object reference, changes to m
in the subclass will be reflected in the superclass; however, this would not have happened if the class member were primitive. (Given that all primitives have an equivalent object, this can be worked out if a little clumsy)
The subclass should also receive the link directly, since where the copy of the link is stored. For abstract superclasses, this is fine, since you are guaranteed a subclass, but below you have to be careful how you process the data.
source share