• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

extending an inner class

 
Tualha Khan
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,
Hey,
I want to know, how can one extend a non static inner class in some package level class. Would appreciate a sample code.
Bye,
Tualha Khan
 
natarajan meghanathan
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No.
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?
TIA
 
Tualha Khan
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!!
Bye,
Tualha Khan
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Val Dra
Ranch Hand
Posts: 439
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jonathan Li
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 {
Test(){
new Outer().super();
}
public static void main(String args[]){
new Test().display();
}
}
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic