Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can a class extend a inner class?

 
Reguri Praveen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • 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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic