File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes How to extend inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to extend inner class" Watch "How to extend inner class" New topic

How to extend inner class

Subhash Pavuskar
Ranch Hand

Joined: Jun 29, 2011
Posts: 57

Hello Everyone !!

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

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46344
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

Joined: Oct 31, 2012
Posts: 9
Hello, please see if the code snippet helps

PS: InnerExtended needs to be inside a class extending Parent. In this case Child.
Henry Wong

Joined: Sep 28, 2004
Posts: 20517

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,

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Winston Gutkowski

Joined: Mar 17, 2011
Posts: 8927

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).


Bats fly at night, 'cause they aren't we. And if we tried, we'd hit a tree -- Ogden Nash (or should've been).
Articles by Winston can be found here
Kemal Sokolovic

Joined: Jun 19, 2010
Posts: 825

Abhijit Durge please UseCodeTags and format your code accordingly.

The quieter you are, the more you are able to hear.
I agree. Here's the link:
subject: How to extend inner class
It's not a secret anymore!