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

valid constructor of inner class

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Please do not shoot the fish in this barrel. But you can shoot at this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic