This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes FYI - Subclassing from an Inner Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "FYI - Subclassing from an Inner Class" Watch "FYI - Subclassing from an Inner Class" New topic

FYI - Subclassing from an Inner Class

Morteza Manavi-Parast
Ranch Hand

Joined: Dec 25, 2008
Posts: 66
Consider the following code:

This code might looks good at first but if you try to compile it, it will fail: an enclosing instance that contains Outer.Inner is required

class SubInner extends Outer.Inner {
1 error

This error is because the no-arg constructor of the class Inner (which is the super class at our case) needs an instance of its enclosing class to bind to. Since we know that inner classes cannot leave without their enclosing class instance so you can not call the constructor of the inner class without passing a reference to its enclosed class. So we fix the above code by a special yet might be wierd syntax like this:

in fact this syntax:


in the sub class constructor means that as well as we explicitly call the super class constructor, we send a reference to the enclosed (outer) object so that the inner class can bind to it.

However there is one exception to this:

In this case the compiler is happy since the class SubOuter is instantiated and passes itself to the SubInner constructor and will pass up all the way to the Inner constructor so the Inner class object (the super class) can bind to that implicit SubOuter object reference. In fact the SubOuter object acts on behalf of the Outer object (the super class enclosing class)

Ok, but it's not finished yet! In the first case, if the Inner class does not have a no-arg constructor and instead has a non-default constructor, things get tricky a little bit:

Got the point? We have to create a non-default constructor for the sub class which has arguments including all of the declared super inner class constructor as well as a reference to the enclosing class!

Ankit Garg

Joined: Aug 03, 2008
Posts: 9291

Seems like I'm not the only one mad about inner classes . Great work Morteza

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
Nice, decription, however I must ask, the need for an inner class to extend its outer class or any other inner-classes as an inner class has access to all memberd of its enclosing class? The only reason that comes to mind is if the inner class you are extending has some behaviour unique to itself (perhaps an event listener) that the sub inner wishes also to make use of?

be a well encapsulated person, don't expose your privates, unless you public void getWife()!
Morteza Manavi-Parast
Ranch Hand

Joined: Dec 25, 2008
Posts: 66
Well, honestly I cannot think of any design scenario that you may want to inherit from an innerclass like the way I decribed. And I don't think what you did mentioned is one of those cases, since if you want to use its "unique behaviour" you can simply use it itself and why would you need to extend it? In my opinion the best use that I can think of is for exam developers to put you in trouble at the exam by this wierd syntax!

Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
he he, i hope not!
Morteza Manavi-Parast
Ranch Hand

Joined: Dec 25, 2008
Posts: 66
Me too!
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Nice work, Morteza!

All code in my posts, unless a source is explicitly mentioned, is my own.
khaled Jamal
Ranch Hand

Joined: Feb 16, 2009
Posts: 64

That's Great, I was convainced non static inner classes could never be subclassed, now I SEE

Thank you so much


I agree. Here's the link:
subject: FYI - Subclassing from an Inner Class
Similar Threads
Subclass of inner class
Questions on innerclass and interface
Inner class references (this & super)
Inner Class/Overridding
Can inner classes be overidden ?