• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inherit an inner class  RSS feed

 
ying lam
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Can you please tell me how can I inherit from an inner class of another class?



I get an error saying "No enclosing instance of type A is assessible to invoke the super constructor and explicitly qualify its super constructor innvocation with an instance of A.

Thank you.
 
W. Joe Smith
Ranch Hand
Posts: 710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Being the extreme expert in Java that I am (all of 2 months) I don't think you can can have a class inherit from an inner class if it is not also an inner class of the same class (if at all). Since an inner class has its lifecycle defined by the outer class and classes that inherit from it would have the same status, I believe. Again, I am not an expert but this is the way it appears to me. I could be wrong.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try changing it to a nested class (labelled "static"). That might work.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, for a start you have to extend A.InnerA, since that's the official name of the class.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Yes, it is possible for an inner class of one class to extend the inner class of another class -- but it is convoluted, so don't do it.

Why? An inner class doesn't exist outside the context of an outer class. So, when an inner class inherits from another inner class, you have to have both the outer classes in context. The syntax is also weird too.

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The syntax is also weird too.


But if you are interested, it looks something like this....



And instantiations of the inner classes look something like this...



Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


BTW, forgot to mention.... If the outer classes inherit from each other, then it is less convoluted.



Henry
 
Ankit Garg
Sheriff
Posts: 9610
37
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just take a look here...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!