aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Can a class extend a inner class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Can a class extend a inner class?" Watch "Can a class extend a inner class?" New topic
Author

Can a class extend a inner class?

Reguri Praveen
Greenhorn

Joined: Feb 19, 2006
Posts: 3
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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Aug 31, 2004
Posts: 11343

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 ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can a class extend a inner class?