Domanda

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

        }
}
È stato utile?

Soluzione

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;
    ...
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top