• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Inner Classes

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
}
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Girish & Balaji
reply
    Bookmark Topic Watch Topic
  • New Topic