me_digvijay
me_digvijay

Reputation: 5492

How does extending outer class works in java

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

        }
}

Upvotes: 1

Views: 1868

Answers (1)

JB Nizet
JB Nizet

Reputation: 691655

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;
    ...
}

Upvotes: 2

Related Questions