1

Referring to the code in this Question I wanted to how know does the extending of an outer class works. What I mean is How can a class (the inner class) can have its definition at two places (in the outer class due to being inner class and in itself due to extending the outer class). What goes inside when this is done.

Thank you

The code

public class Something {
    private int y;

        class IsSomething extends Something {

            public void print() {
                System.out.println("123");
            }

        }
}
Community
  • 1
  • 1
me_digvijay
  • 5,374
  • 9
  • 46
  • 83
  • Your question doesn't make sense. The inner class is defined in one place: in the inner class. – user207421 Apr 15 '13 at 06:19
  • @EJP: But if the inner class extends the outer class all the members (except private) of the outer class (including the inner class itself) can be used by the extending class. So this looks like the inner class can access itself via the outer class. – me_digvijay Apr 15 '13 at 06:26
  • Still not clear. Show us a code example. – TheBlastOne Apr 15 '13 at 06:31
  • @TheBlastOne: I have added the code in the question. – me_digvijay Apr 15 '13 at 06:35
  • I see. So where´s the question? What does not work? As noted, the inner function is defined exactly once. Also, I wonder inhowfar an inner class that extends its outer class could be useful :? – TheBlastOne Apr 15 '13 at 06:40
  • @TheBlastOne: Its not that something is not working. I just wanted to know what happens when an object of the inner class is created. This object can access the member of the outer class (being an inner class of it) and can access itself being the subclass of the outer one. So which fact will be used to access the members of the outer class: being an inner class or being the subclass? – me_digvijay Apr 15 '13 at 07:03
  • Well, the this instance "variable" is being used to access the current instance´s members, as usual. So if the current instance inherits from the outer class in this case, then a) who cares? b) what would that be good for? – TheBlastOne Apr 15 '13 at 07:06
  • 1
    The inner class **declaration** can be seen as a `static final` field and as such the inner class cannot "access itself" anymore than it can access any other class. When you create an instance of the inner class it contains all of its own fields and a *reference* to an instance of the outer class. However the outer class *instance* does not contain a reference to the inner instance you just created. – Sebastian Apr 15 '13 at 07:32

1 Answers1

2

An inner class has a reference to an instance of its outer class. This is a has-a relationship.

If it extends its outer class, it also has a is-a relationship wit its outer class.

So it's equivalent to the following two top-level classes:

public class Foo {
    ...
}

public class Bar extends Foo {
    private Foo outerFoo;
    ...
}
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • So the outerFoo instance will not have any reference to the object of the Bar class? – me_digvijay Apr 17 '13 at 05:25
  • No, unless it explicitely call its constructor and assigns the created Bar object to a field. – JB Nizet Apr 17 '13 at 06:44
  • If one understands that, *As far as the virtual machine is concerned all classes are top level classes no special support is required for nested classes. Nesting is all handled by compiler transformation.* Then things would be clear on understanding *nested* & *inner* classes. – overexchange Sep 30 '15 at 15:29