This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes extending an inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "extending an inner class" Watch "extending an inner class" New topic

extending an inner class

Tualha Khan
Ranch Hand

Joined: Nov 22, 2000
Posts: 287
Dear All,
I want to know, how can one extend a non static inner class in some package level class. Would appreciate a sample code.
Tualha Khan

SCJP2, BEA WLS 6.0, DB2 UDB 7.1
natarajan meghanathan
Ranch Hand

Joined: Feb 01, 2001
Posts: 130
You cannot extend a non-static inner class in a package level class. The problem comes in creating a default constructor for the super class in the constructor of the class u r extending. The superclass constructor in this case cannot be just called in this case super() or any other way.
I think i am right. Can any one confirm it?

Sun Certified Programmer for Java 2 Platform
Tualha Khan
Ranch Hand

Joined: Nov 22, 2000
Posts: 287
But, in KAM Page no. 235, first paragraph headed " Inheritance and Containment Heirarchy of Non Static Inner classes" says, that
" Non staticinner classes can extends other classes and can themselves be extended.Therefore, both the inheritance and containment heirarchy must be considered when dealing with member access." And then the book gives an example of an inner class extending some other class. Please note that the author says that "non static inner classes can themselves be extended."
So is there some way to do it!!!
Tualha Khan
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi Tualha,
I don't think there is a way to directly extend a non-static Inner class as you must have a reference to the enclosing class.
For example, if you have class Outer which contains class Inner and you try: class ExtendInner extends Outer.Inner, you get a compile error.
You can, however, use an Anonymous class to extend Inner

Jane Griscti
Sun Certified Java 2 Programmer

Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Val Dra
Ranch Hand

Joined: Jan 26, 2001
Posts: 439
You right you can't directly extend it without providing a reference to an external class. This code first extends the outer class which provides a reference for the inclosing class

Val SCJP <BR>going for SCJD
Jonathan Li

Joined: Feb 05, 2001
Posts: 4
well so it seems that top-most classes can't extend inner classes because there's no way to obtain an enclosing instance...but inner classes will indeed be able to extend other inner classes, static or not.
Navin Narayan

Joined: Jan 25, 2001
Posts: 25
Hi Taulha,
The reply is a little late but still. One can extend non-static inner classes. But still the inner class will depend on an instance of the outer class. Hope the following code helps.
class Outer {
class Inner{
public void display(){
System.out.println("Inner class display method");
class Test extends Outer.Inner {
new Outer().super();
public static void main(String args[]){
new Test().display();
I agree. Here's the link:
subject: extending an inner class
It's not a secret anymore!