Re: Protected inner classes and inheritance
Scott Harper wrote:
[...]
Any other class that didn't inherit from TopLevel wouldn't be able to
reference Inner anyway, so it doesn't matter if its members are public or not.
It's safe, if you do not expose any Inner's instance to the other
classes, and Inner constructors are protected (or private). Otherwise,
public members are accessible.
For example, the following will change 'intField' of a given
TopLevel.Inner instance:
Object innerInstance = ...
innerInstance.getClass().getField("intField")
.setInt(innerInstance, 100);
Just for grins, I made Inner private, and sure enough, even though its members
are still public, the inheriting class (SecondLevel) can't resolve them.
Even when class is private, reflective access to its public members is
still allowed -- standard Java security mechanisms (SecuritManager
enabled, etc.) do not prohibit that.
Of course, my first statement from this post still holds here, if you
protect an instance and its class' constructors, you are safe.
piotr