• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Extending Inner Classes

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Sun Press Book The Java Programming Language Third Edition:
For
class Outer{
class Inner{
}
}
It is said for extending inner class that
<Quote>
If the enclosing class of the inner subclass is not a subclass of Outer, or if the inner subclass is not itself an inner class, then an explicit reference to an object of Outer must be supplied when the Inner constructor is invoked via super.
</Quote>
They have given follwing code for extending
class Unrelated extends Outer.Inner{
Unrelated(Outer ref){
ref.super();
}
}
I can not understand why in the constructor there is need to call ref.super() ???
Reference: Chapter 5 section 5.2.2 Page 127.
 
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kartik,
This question of yours is really a good one.
Think of every object as a so-called "Russian doll". For example consider an instance of Unrelated; inside it it "encapsulates" an instance of Outer.Inner; which inside it still contains an Object.
When you are constructing an Unrelated, an "encapsulated" (in lack of a better term) Outer.Inner will also be created. Hence, the explicit call to Outer.Inner's constructor (via super). Keep in mind however that, at the same time, the "encapsulated" instance of Outer.Inner *needs* a reference to an Outer. (Outer.Inner being an inner class).
If we didn't supply a qualifier of type Outer to the super() expression, how would we know which the enclosing instance of Outer would be? Note that Unrelated is (as it names cries out) "unrelated"; were it a subclass of Outer it could (implicitly) serve as the enclosing instance. Now, it being unrelated, we need an explicit reference to an Outer.
Panagiotis.
=====================
class Unrelated extends Outer.Inner{
Unrelated(Outer ref){
ref.super();
}
}

I can not understand why in the constructor there is need to call ref.super() ???
=====================
 
Kartik Shah
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agreed Panagiotis,
I understand that the instantiation of Unrelated requires an instance of Outer.Inner which in turn requires instance of Outer. Perfectly fine with that thougt. We are on same path of thinking.
Further my question is why we need to call ref.super();
I agree that Outer is implicitly derived from Object but why is that need to call ref.super() even though compiler places super() before the constructor of Outer.
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kartik,
I kind of agree with you. Since the instance of Outer is given as the paramter of the Unrelated constructor, the compiler should be able find the instance of Outer. I am sure that there is some reasons ref.super() has to be called because Sun states in their book. It is not easy to writer a language compiler.
Zheng
reply
    Bookmark Topic Watch Topic
  • New Topic