Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Extending a second Inner Class

 
Milton Ochoa
Ranch Hand
Posts: 336
Firefox Browser Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

Today I was studying about Inner Class, and I trying a few combinations with the classes. Then a found this:




this is the output that the compiler shows:



This is very rare, why the second inner class cannot extend from the first class?

And if someone can explain me the compile error.

Thank you
 
Campbell Ritchie
Sheriff
Posts: 51453
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What sort of constructor do you have in the superclass? Are you required to call in with a super(x, y, z); statement?
 
Milton Ochoa
Ranch Hand
Posts: 336
Firefox Browser Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Campbell.

I dont understand what are you question me!

can you explain more please!
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The inner inner class implicitly has a reference to an instance of the inner and outer class.

Also, the inner inner class [default] constructor needs to call the inner class [default] constructor, as its superclass constructor.

Somehow, in a way that I haven't exactly worked out, the compiler needs to access the instance of the inner class before the inner class constructor has been called, as the superclass constructor.

To be honest, I don't think there is a huge amount to be gained by deep analysis of what is going on here (which is why I haven't bothered!). I don't believe there would ever be a good reason to have an inner class that extends its enclosing class. So just Don't Do That Then.
 
Brian Cole
Author
Ranch Hand
Posts: 912
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that if either of the nested classes is declared to be static, or either is declared with a different superclass, then it compiles without error. But where's the fun in that.

It will also compile if we use a qualified superclass constructor invocation:

Or we could write it this way:

I mostly have a handle on why the original doesn't compile, but I'm having trouble putting it in words. (Which means I must not completely understand it.)

(cf. JLS �8.1.3)
[ January 31, 2008: Message edited by: Brian Cole ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic