• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

inner classes/inheritance

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
This what I think ...
1> Z is trying to upcast himself and use the runtime type of super which has access being in the same namespace as X (X.Y.f())
2> Same reason but a bit baffled here.
Sandeep
 
reply
    Bookmark Topic Watch Topic
  • New Topic