• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about Inner class

 
L Yan
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
only if Inner is static class can you use Outer.Inner.
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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 ]
 
L Yan
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very clear. Thanks a lot.
 
pranav thakker
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


regards
[ July 21, 2006: Message edited by: Naseem Khan ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic