C
Christian Bongiorno
I didn't come up with this question, but I saw it elsewhere and I am
curious. I believe line 1 compiles fine because the code is upcasting
itself. Line 2.... That one evades me
------------------------------------------------
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
curious. I believe line 1 compiles fine because the code is upcasting
itself. Line 2.... That one evades me
------------------------------------------------
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