File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes valid constructor of inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "valid constructor of inner class" Watch "valid constructor of inner class" New topic
Author

valid constructor of inner class

Bret Waldow
Ranch Hand

Joined: Aug 04, 2000
Posts: 59
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
rajsim
Ranch Hand

Joined: May 31, 2000
Posts: 116
As inner class is being extended outside its enclosing class,
you have to pass a reference to the enclosing class in its
constructor.
This concept is 99% out of scope of the SCJP test. Also, the
guide you are studying is covering many more such out of scope
topics.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: valid constructor of inner class