Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

FYI - Subclassing from an Inner Class

 
Morteza Manavi-Parast
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!







 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems like I'm not the only one mad about inner classes . Great work Morteza
 
Stephen Davies
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
Morteza Manavi-Parast
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 352
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
he he, i hope not!
 
Morteza Manavi-Parast
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Me too!
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nice work, Morteza!
 
khaled Jamal
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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

Thank you so much
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic