Question

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");
            }

        }
}
Was it helpful?

Solution

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;
    ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top