aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question about 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 "Question about Inner class" Watch "Question about Inner class" New topic
Author

Question about Inner class

L Yan
Greenhorn

Joined: Apr 25, 2006
Posts: 18
Code:

class Outer {class Inner {} }

public class InheritInner extends Outer.Inner
{
InheritInner() {}
public static void main (String[] args)
{
Outer o = new Outer();
InheritInner ii = new InheritInner();
}
}

Compile error is:
InheritInner.java:5: an enclosing instance that contains Outer.Inner is required

InheritInner() {}
^
Don't know why? Thanks.
warren li
Ranch Hand

Joined: May 23, 2006
Posts: 128
only if Inner is static class can you use Outer.Inner.


SCBCD 1.3: 94%<br />SCWCD 1.4: 91%<br />SCJP 5: 95%
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809


At line 1, basically two objects are created.

First of class A, then of your class B. So basically you have two instances here and instance of A act as a subobject which is actually wrapped within intstance of subclass.

Similary in your code, an instance of InheritInner must have an instance of Inner class and you know that Inner class always require an instance of Outer class.

But in your code, there is not instance of Outer class used in creation of Inner instance. Although you hav created, but you hav not used.

Modify your code as below...



Here At line o.super() before call to Super class constructor(i.e. Inner class constructor), you already hav a Outer instance o as a context.

You can also make your Inner class static without altering any further code. But in that case, its not actually Inner class rather its better to name it Nested class.





Regards

Naseem

[Message edited: Used Real words]
[ July 21, 2006: Message edited by: Naseem Khan ]

Asking Smart Questions FAQ - How To Put Your Code In Code Tags
L Yan
Greenhorn

Joined: Apr 25, 2006
Posts: 18
Very clear. Thanks a lot.
pranav thakker
Greenhorn

Joined: Nov 21, 2005
Posts: 20
Hi Naseem,

Can you please explain this statement in detail ?

Here At line o.super() before call to Super class constructor(i.e. Inner class constructor), u already hav a Outer instance o as a context.


Thanks,

Pranav Thakker
Ashish Laddha
Greenhorn

Joined: Apr 12, 2005
Posts: 21
Here At line o.super() before call to Super class constructor(i.e. Inner class constructor), u already hav a Outer instance o as a context.


Can you explain how the context is formed and o becomes available to it.

Thanks in advance.
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809


At line 1, Outer instance o is required in creating an instance of Inner class.

Similary here in o.super(); o is required in creating an instance of Inner class.

super() will call super class constructor and it creates an instance of Super class (Inner class), but since super class is an Inner class so o is required before instantiation jst like in above code.

regards
[ June 12, 2006: Message edited by: Naseem Khan ]
Ashish Laddha
Greenhorn

Joined: Apr 12, 2005
Posts: 21
The point which is clear is
-> For creation of Inner, we need an instance of Outer.

The point which is not clear is
-> By calling o.super(), how are we making instance of Outer available for creation of instance of Inner

I guess, you got the doubt.
Nikhil Goel
Ranch Hand

Joined: Jul 11, 2003
Posts: 32
I personally tried the code mentioned by Nadeem and it works . My take on this if you want to access InnerClass1 you need to first create the object of the class in which it is nested .
The code correction provided by Nadeem does that .
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809


regards
[ July 21, 2006: Message edited by: Naseem Khan ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question about Inner class