I am working with Global Knowledge Certification Press's Sun Certified Programmer for
Java 2 Study Guide. This is a modification of question 13 of Chapter 8.
One of my practice exams presents this question. Given:
class EnclosingClass {
public class Inner {
Inner(int a)
{...}
}
public class Inner2 {
Inner2(int a)
{...}
}
}
class SubClassOfInner extends EnclosingClass.Inner {
// how to write a constructor?
}
What would be a valid constructor?
They also provide this answer:
SubClassOfInner(EnclosingClass encloser, int a) { encloser.super(a); }
I don't understand this. I know that Inner is a non-static member inner class, and thus can be instantiated from outside EnclosingClass. Since it is non-static, it requires an instance of EnclosingClass to instantiate it, which is provided by the encloser object.
What I don't understand is what this constructor code does. It looks like the code calls the superclass(int) constructor for EnclosingClass (presumably Object if EnclosingClass is not extending something else in between). How does this code construct an Inner object? How does it differentiate from an Inner2 object?
In the discussion earlier in the chapter, the author suggests that 'EnclosingClass.super' will refer to the superclas of EnclosingClass, if called from within the body of the Inner class. This fits with my expectation. The example above seems a different situation, but I don't understand how the construct mutates to look like a call to the EnclosingClass super, while actually calling the Inner class's extension's super! What if I did want to call the EnclosingClass's actual super() from this context? There seems to be a naming collision?
Thanks in advance for any help.
regards,
Bret