• 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
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Question about Inner class

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
only if Inner is static class can you use Outer.Inner.
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very clear. Thanks a lot.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


regards
[ July 21, 2006: Message edited by: Naseem Khan ]
 
Live ordinary life in an extraordinary way. Details embedded in this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic