• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inner Classes

 
Sharn Arora
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Following code gives compile time error on line 6
InheritInner.java:6: an enclosing instance that contains Outer.Inner is required.

Can anyone explain me the cause of the error and how to rectify it. Thanks.


 
Girish Nagaraj
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Outer {

class Inner { }
}

class InheritInner extends Outer.Inner {

InheritInner() {new Outer().super();}

public static void main(String[ ] args) {

Outer o = new Outer( );

InheritInner ii = new InheritInner( );
}
}

OR

class InheritInner extends Outer.Inner {

InheritInner(Outer o) {o.super();}

public static void main(String[ ] args) {

Outer o = new Outer();

InheritInner ii = new InheritInner(o);
}
}
 
Balaji VR
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here we are trying to create a subclass for an Inner class. The complier obviously complains here because--->> When we try to create an object for "InheritInner", then it has to initialize "InheritInner" class AND all of its superclasses in the hierarchy!

Here in our case, the complier cant initialize a member (i.e the inner class "Inner" which is actually a part of another class i.e "Outer")

Hence the error!
 
Sharn Arora
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Girish & Balaji
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic