I would like to declare a data member of a superclass, private:
public abstract class superclass {
private int verySensitive;
abstract int setVerySensitive(int val); // must be overriden by subclass to work properly
}
public class subclass extends superclass {
@Override
protected int setVerySensitive(int val) {
if (val > threshLow && val < threshHigh) // threshHigh is calculated in superclass constructor
verySensitive = val;
}
}
As you can see, I have a problem here: superclass can’t access verySensitive because it’s private, but I don’t want to make verySensitive protected because it’s… sensitive.
Also note that setVerySensitive was made abstract because checking against valid values can only be done after superclass has been constructed.
Can you recommend an elegant way of getting out of this “catch 22” situation?
I suggest something like this:
This is similar to EboMike’s suggestion, but it leaves
setVerySensitive(int)with package access instead of making it private.