This construct just does not compile:
class A {
private int data;
public static int process(B b) {
return b.data;
}
}
class B extends A {}
Of course, this problem can be solved easily by hand (cast b in A, field protection, etc.). But the question is, why does Java not allow such a design? I thought that the compiler should know that B is a subclass of A, so methods A should have access to private fields.
The only possible problem that I can think of is that if B has its own data field, the compiler does not need to know in which field we want to access, but for what we inherit, right?
source
share