Dobieslaw said:
Does derived function f accept parameters of A1.Subcl and iterpret their
fields/methods (inherited or overriden from A0.Subcl) correctly?
Seemingly not. And it took me a while to understand why.
I used your code to create this file:
/* BEGIN CODE */
class A0 {
A0() {
System.out.println("In A0 constructor");
}
void f(Subcl s) {
System.out.println("In f with " + s);
System.out.println("Inner class instance value is "
+ s.instanceValue);
}
class Subcl {
int instanceValue = 3;
Subcl() {
System.out.println("In A0.Subcl constructor");
}
}
}
class A1 extends A0 {
A1() {
System.out.println("In A1 constructor");
}
class Subcl extends A0.Subcl {
int instanceValue = 6;
int otherValue = 9;
Subcl() {
instanceValue = 6;
System.out.println("In A1.Subcl constructor");
}
}
}
public class InnerClassExtension {
public static void main(String [] args) {
A1 a = new A1();
A1.Subcl s = a.new Subcl();
a.f(s);
System.out.println("s.instanceValue is "
+ s.instanceValue);
System.out.println("s.otherValue is "
+ s.otherValue);
}
}
/* END CODE */
which compiles fine, and the output of running it gives:
In A0 constructor
In A1 constructor
In A0.Subcl constructor
In A1.Subcl constructor
In f with A1$Subcl@194df86
Inner class instance value is 3
s.instanceValue is 6
s.otherValue is 9
So method f(Subcl s) *does* have access to the A1.Subcl instance, but
because A1.Subcl is an extension of A0.Subcl, A1.Subcl also has access
to a superclass instance. (Because a constructor always calls its
superclass constructor first.)
Which means that when you call method f on an instance of A1.Subcl, it
actually refers to the instance variables of the instance of A0.Subcl,
which means that it probably is not accessing the values you were hoping
it would access.
Someone please correct me if I'm wrong. (Anyone recommend a good book on
the perils of inheritance?)