This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Extending a second Inner Class" Watch "Extending a second Inner Class" New topic
Author

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
Sheriff

Joined: Oct 13, 2005
Posts: 36482
    
  16
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
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
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: http://aspose.com/file-tools
 
subject: Extending a second Inner Class
 
Similar Threads
innerclass
Inherting Static classes
Q of Khalid's inner class example
Q: Anonymous inner class extending outer class
Inner Class