class One {
void foo() { }
}
class Two extends One {
private void foo() { /* more code here */ }
}
Why is the above snippet of code wrong?<
The problem with this code is that if it were legal, Java wouldn't be able to respect the private
modifier of foo
if you accessed it indirectly through the One
base class. For example, if I were to write
One obj = new Two();
obj.foo();
Then we'd be in trouble because we'd be calling the private
method foo
of Two
indirectly, since when the compiler checks the line obj.foo()
it looks at One
to determine if foo
is accessible, not at Two
. The reason for this is that the compiler can't always tell what obj
could be pointing at - if, for example, I write something like
One obj = Math.random() < 0.5? new One() : new Two();
obj.foo();
Then the compiler can't know whether obj
points at a One
or a Two
. Consequently, it defers to One
when checking access specifiers. If we were indeed allowed to mark foo
private in Two
, then the compiler would incorrectly allow us to call it through obj
, which has type One
, bypassing the guarantee that only the object itself can call private
methods.