posted 20 years ago
I posted the same q a week ago, but nobody came up with an explanation
Can please someone shed some light on it...
Give the code below:
public class X {
class Y {
private void f (){}
}
class Z extends Y {
{ //initializer
//f(); // error; not inherited
//this.f(); // error; not inherited
((Y)this).f(); // 1 - OK access granted
super.f(); // 2- OK access granted
}//end initializer
}//end class Z
}// end class X
it compiles fine.
The commented statements do not compile because method Y.f(), being private is not inherited in class Z private
Please tell if I’m correct in my reasoning:
For statement 1 to compile two conditions must be satisfied:
A) both classes must be enclosed by a third (they can access each others private members)
B) class Z has to extend class Y
But I do not understand why it works
Can someone tell why the statemnent 2 also compiles
Thanx
"Did anyone understand what I have just explained? ... because I did not!"