• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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
Give the code below:

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 statements 1 to compile two conditions must be statisfied:
A) both classes must be enclosed by a third (they can access each others private members)
B) class Z has to extend class Y
The expression((Y)this).f() refers to method f() of the current object, but with the current object viewed as an instance of the superclass (because of the casting). Method f() is accessible because condition A is satisfied so the statement compiles
Please tell me if I missed something
Can someone tell why the statemnent 2 compiles

(code tags and indentation added)
[ May 02, 2004: Message edited by: Barry Gaunt ]
[ May 02, 2004: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a tricky one. I am suprised that //1 and //2 compile. Z is inheriting from Y and f() is private in Y so it should (in my opinion) not be possible for Z to access it. I'm going to have to research this one, but hopefully one of our regular gurus will step in and explain it all.
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a thought: if Z does not extend Y you could still use the method f like this
new Y().f();
 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Lionel Orellana:
Just a thought: if Z does not extend Y you could still use the method f like this
new Y().f();


loud thinking...
Its almost as if private void f() is regarded as just another member of the enclosing class X...
 
Dan Andrei
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"almost" is the key here Bose, thats why I posted the piece of code
using the f() method call doesn't intrigue since even if its private we are in an inner class, so its allowed
The super call is the one which I cannot "grasp", how the compiler "looks" at it at "says" its ok ??,
AFTER ALL ITS CLEAR THAT F() IS NOT INHERITED AS I SHOWED ON THE COMMENTED LINES
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
}
}

The above is your posted code.
Before answering to your question I do want to know do you know that in java what is meant by not mentioning any access specifier. This is default access modifer. It meant that the members can be accessed within the same package, any subclass and non-subclass within the same package.
And also when a class is inner to another class it has access to all of the members of the outer class(here members must not be private) but the reverse is not legal.
O.K. Let me to come to your comments on 2 compiled statements.
((y)this).f() - here the code before the period will create anonymous object of type y and the statement is equivalent to creating a new object of type y and calling function f through this object. If f() is not private you can directly call it from the inner class z.
super.f() - super is the keyword to refer to the immediate outer base class in inheritance. so here it is actually calling function f of object of type y.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic