Interesting Inheritance question with Inner classes

  • Thread starter Christian Bongiorno
  • Start date
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
 
J

Jesper Nordenberg

Christian Bongiorno said:
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
Correct.

But I do not understand why it works

It works because everything inside class X can access the private
methods of Y and all other inner classes inside X. Even if Z doesn't
inherit from Y you could still do:

new Y().f();

inside a method in Z.

The casting to Y tells the compiler to look for methods in Y. You
can't use 'this.f()' because private methods are not inherited to sub
classes and 'this' is of type Z, not of type Y.
Can someone tell why the statemnent 2 also compiles

Statement 2 compiles because 'super' is of type Y and you're allowed
to call the private method (see above). You have to use super because
private methods are not inherited.

Actually this is just a type issue in the Java compiler, the JVM has
no concept of inner classes.

/Jesper Nordenberg
 
F

Fahd Shariff

super allows you to refer to things (variables and methods) in the
superclass. So super.f() tries to execute a method f() in the
superclass Y and succeeds. Thats what i think, although maybe i am
simplifying things a bit :)
Any other ideas?

Fahd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top