• 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
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Can a class extend a inner class?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. class Outer {
2. class Inner { }
3. }
4.
5. public class InheritInner extends Outer.Inner {
6. InheritInner () { }
7.
8. public static void main(String [ ] args) {
9. Outer o = new Outer( ) ;
10. InheritInner ii = new InheritInner( ) ;
11. }

The above code is from http://www.akgupta.com/Java/mock_exam.htm mock exam
Qno 38

It throws a error at compile time saying that
InheritInner.java:7: an enclosing instance that contains Outer.Inner is required
InheritInner () {


My question is can we inherit a inner class? If we can then why need the concept of inner class?

Thanks in advance for the help
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An inner class is normally only used with it's outer class. You can create instances of it in other classes by using an instance of the outer class. You can extend the inner class in other inner classes in the same outer class.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An inner class (a nested class that is not static) is tied to an instance of its enclosing class.

However, note that inner classes can inherit independently from the outer class. When a base class is extended and its inner classes are re-declared in the subclass (similar to a shadowed variable), these nested classes do not automatically inherit from the derived class.
[ February 26, 2006: Message edited by: marc weber ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic