• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

confusion in inner classes

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Outer extends X
{
class Inner
{
}
}
class Unrelated extends Outer.Inner
{
Unrelated(Outer ref)
{
ref.super();
}
}
the above case is a slight modification to the one on page 126(Extending Inner Classes) in Java Programming Language (third edition) by james gosling.
the confusion is shoulnt the "ref.super()" have "ref.this()" instead ...as super of the Outer class will refer to the constructor of class X
plz suggest,
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Deependra, before I answer your question, I want to show you something that helps me to think about this special example.
Here is the class hierarchy. Outer extends X and Unrelated extends Outer.Inner.

The important thing to notice in order to answer your question is that Outer.Inner has a (secret/synthetic/compiler generated) reference to Outer.

Compiling this code generates 4 class files.
X.class
Outer.class
Outer$Inner.class
Unrelated.class
Let�s disassemble the class file Outer$Inner.class

Notice that Outer.Inner has a reference to Outer.
Now to answer your question...
We know that the constructor for the class Unrelated must invoke some constructor of the superclass Outer.Inner.
If we said this: class Unrelated { Unrelated() { super(); } }
then the superclass would not have a reference to Outer.
If we say this: class Unrelated { Unrelated() { outer.super(); } }
then we are giving the superclass a reference to Outer.
The syntax super() invokes the no-arg superclass constructor. The special syntax ref.super() invokes the no-arg superclass constructor and also gives the superclass a reference to the enclosing class.
[ November 02, 2003: Message edited by: Marlene Miller ]
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

the confusion is shoulnt the "ref.super()" have "ref.this()" instead ...as super of the Outer class will refer to the constructor of class X


Hi Deependra, this time, I will try to answer your question directly.
If super() is declared in the constructor of class Unrelated, it invokes a constructor of the superclass of Unrelated, namely Inner.
If this() is declared in the constructor of class Unrelated, it invokes another constructor of the class Unrelated.
A constructor of Unrelated cannot invoke a constructor of Outer or a constructor of X, because Unrelated does not extend Outer or X.
 
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Deependra
The compiler error on this specific example is very helpful too. You might wanna try that.
Regards
Harwinder
 
Deependra Sharma
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot miller
u really explanined it well as well gave me additional knowledge.
what is the complile time error plz tell me-harwindar?
 
I've never won anything before. Not even a tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic