Re: Accessing private member via subclass
Arne Vajh?j wrote:
Mike Schilling wrote:
I thought I understood Java's access control rules pretty well, but
this case puzzles me.
public abstract class Super
{
private int i;
void method(Sub s)
{
s.i = 2; // (*)
}
}
public class Sub extends Super
{
}
Any thoughts about this?
Given that Super class should not refer to Sub class,
then this seems to be an intellectual exercise.
As I said upthread, it came up during normal development. (For some value
of "normal".)
I have some code that generates Java classes from XMLSchema types. One of
its features is that the generated classes can be customized. If the
generator, which is about to create XXX.java, notices that there's a
hand-coded XXX.java, it instead generates an abstract base class
XXX_Base.java.
(The handcoded XXX is expected to anticipate this and extend XXX_Base).
Now, if the generated XXX_Base includes a factory method, it will of course
create XXX's (since you can't create XXX_Base's, which are abstract), and
might decide to mess with their private parts.
Having run into this isue, it was trivial to generate
XXX_Base instance = new XXX();
instead of
XXX instance = new XXX();
but, as I said, I was at first puzzled by the need to do this.