File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Extending a second Inner Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Extending a second Inner Class" Watch "Extending a second Inner Class" New topic

Extending a second Inner Class

Milton Ochoa
Ranch Hand

Joined: Oct 23, 2007
Posts: 336

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

Joined: Oct 13, 2005
Posts: 46367
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

Joined: Oct 23, 2007
Posts: 336

Hi Campbell.

I dont understand what are you question me!

can you explain more please!
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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.

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Brian Cole
Ranch Hand

Joined: Sep 20, 2005
Posts: 878
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 ]

bitguru blog
I agree. Here's the link:
subject: Extending a second Inner Class
It's not a secret anymore!