Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to extend inner class

 
Subhash Pavuskar
Ranch Hand
Posts: 57
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone !!

Can anyone please explain is that possible to extend inner class if yes then please give me the snippet of code.

Thanks
 
Campbell Ritchie
Sheriff
Posts: 48453
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That question is vague. Is it a private or public inner class? Do you want to extend it inside the class or in another class? Why would you want to? What have ou tried so far?
 
Abhijit Durge
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, please see if the code snippet helps




PS: InnerExtended needs to be inside a class extending Parent. In this case Child.
 
Henry Wong
author
Marshal
Pie
Posts: 20907
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhijit Durge wrote:
PS: InnerExtended needs to be inside a class extending Parent. In this case Child.


And if you don't want the outer classes to have any relationship whatsoever -- except for the inner class inheriting from each other that is -- then you can do something like this...



[EDIT] Just realized that the OP didn't mention that the inner class has to be extended by another inner inner class. Extending the inner class from a top level class is similar...



Hope this helps,
Henry
 
Winston Gutkowski
Bartender
Pie
Posts: 10111
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhijit Durge wrote:PS: InnerExtended needs to be inside a class extending Parent. In this case Child.

I see nothing wrong with what you've written (apart from maybe your names). However, to make Inner extendable by ANY class that extends Parent, you should really make it protected, viz:
protected class Inner { ...

However, my question would be: How often do you think you're going to need this information? True inner classes are rare enough, and their use tends to suggest finality, not extension. If it's for the SCJP exam, then good luck with that; but the chances of you ever needing to use it in real life are slim to none (in eleven years of Java programming, I haven't).

Winston
 
Kemal Sokolovic
Bartender
Posts: 825
5
Java Python Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhijit Durge please UseCodeTags and format your code accordingly.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic