aspose file tools*
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "FYI - Subclassing from an Inner Class" Watch "FYI - Subclassing from an Inner Class" New topic
Author

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:

InheritFromNonStaticMemberClass.java:14: 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:

outer.super();

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!








SCJP 6
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

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


SCJP 6, SCBCD 5, SCWCD 5



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: FYI - Subclassing from an Inner Class