• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

compile time error

 
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Outer{
class Inner{}
}
class sub extends Outer.Inner{
sub(){}
public static void main(String args[]){
Outer o=new Outer();
sub s=new sub();
}
Ans given code does not compile.
Could anyone explain why?

 
Ranch Hand
Posts: 1246
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what error did u get??
if u r getting this "no enclosing instance of type Outer is in scope class sub extends Outer.Inner{"
That is a inner class your extending which it is out of scope.
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good Question:
A non-static inner class is nascent. It cannot exist without the outer classes instance. Therefore Extending non-static inner class will be illegal
However if you make Inner class static it works
class Outer{
static class Inner{}
}
class sub extends Outer.Inner{
sub(){}
public static void main(String args[]){
Outer o=new Outer();
sub s=new sub();
}
Why do u think it works now?
Ragu
 
Neha Sawant
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is the problem only of it being static and nonstatic .
Is there no problem of constructors.
I dd not have any idea of constructors in inner class and how they are called from outer class and sub class.
do u guys know any good link where i can get a good idea about this topic
thanx in advance
Neha
 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class sub is a non-static inner class
It exists only in the context of an object of it's enclosing class ( which here is an object of class Outer).
So, when you try to create an object of class sub, instead of using

sub s=new sub();


you should use
sub s = new Outer().new sub();
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Neha,
code
____________________________________________________________
1: class outer {
2: class inner {
3: inner() {
4: System.out.println("Inner class constructor");
5: }
6: }
7:}
8:
9:class sub extends outer.inner {
10:// sub() {}
11:// Constructor must explicity specify a containing instance when invoking the superclass constructor.
12: sub(outer o) {
13: o.super();
14: System.out.println("subclass constructor");
15: }
16:
17: public static void main(String args[]){
18: outer O =new outer();
19:// sub s = new sub();
20: sub s=new sub(O);
21: }
22:}
____________________________________________________________
It is possible for a subclass to extend a non-static inner class. This means that the subclass does not have a containing instance, but its superclass does. When the subclass constructor invokes the superclass constructor, it must specify the containing instance. Please have a look at line no.12, 13 & 20.
I hope this helps.
Regards,
Malar.

[This message has been edited by Malar Ravi (edited November 07, 2001).]
 
Malar Ravi
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Madan,
Here sub is not a non-staic inner class, its just an ordinary top-level class that extends an inner class.
To create an instance of inner (inner class) and not sub (top-level class that extends outer.inner) you should have an instance of its containing class, in this case you should have an instance of outer (outer class).
outer.inner in = new outer().new inner();
Regards,
Malar.
 
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Malar, I shortened up your code a bit. I am totally confused how this is compiling working. Could you or someone maybe shed more light on this. To me it would look like no matter what you do in the constructors you shouldn't be able to extend this non static inner class since you wouldn't have an instance of the Outer class, so that when "extends Outer.Inner" is done how is there any access to Inner without an instance of Outer first being created? I don't really see how the code below changes things since doesn't the super class have to be set up first before any constructors are even called in the sub classes? If that was the case it would look like it shouldn't matter what you do in the base class constructor. Obviously I'm way off here since the code below does compile fine and I know you can extend non static inner classes. I'm just all mixed up right now . Thanks for more clarification. Rick

[This message has been edited by Rick Reumann (edited November 07, 2001).]
 
Rick Reumann
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess even a better question would be will I see this on the test To try to an inherit from a non-static inner class without even an instance of the inner class' enclosing Outer class seems something that would be rarely ever done, and if done to me wouldn't make much sense. In neither of my certification books (RHE, and Mughal's) do I see an example of this type of situation. They do mention of course inheriting from non-static inner classes but always in the context of having an instance of the enclosing class. The above 'trick', as I see it, of using a call to super( SuperclassOfTheInnerClass s ) seems really odd. Am I way off here in thinking this way? Thanks for any help.
Rick
 
Malar Ravi
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rick,
I am not sure if this concept of "extending Inner class" has ever appeared in SCJP exams. Maybe people who have already taken this exam can clarify this.
Yes you are correct, an instance of Outer is required to access Inner. That is why we are creating an instance of Outer and passing the reference to the constructor of Sub as a constructor parameter. This is the only way to get around this problem.
Constructor of Sub is like any other normal derived class will make a call to super() in parent object. Since that parent object (Outer.Inner) is not availabe in Sub, an explicit call to the parent's constructor is made in o.super where o is the reference to the Outer instance passed as constructor argument.
This is my understanding; if anyone has a better explaination please add to this thread.
Regards,
Malar.
 
reply
    Bookmark Topic Watch Topic
  • New Topic