Java: Is the class a subclass of itself?

A class can be a "subclass" of itself if its inner class extends the outer class so the class expands somehow without throwing any Exception. So, does this really mean that the class is also a subclass of itself?

Thanks.

+3
source share
6 answers

A class is not a subclass of itself. An inner class may be a subclass of another class, but it is a separate class. You can verify this by comparing the class instances that you get when you call getClass ().

+6
source

, . , , . , .

+3
public class ParentClass {

    int intField = 10;

    class InnerClass extends ParentClass {

    }

    public static void main(String... args) {
        ParentClass parentClass = new ParentClass();
        InnerClass innerClass = parentClass.new InnerClass();

        System.out.println(innerClass.intField);

        InnerClass innerClass2 = innerClass.new InnerClass();
    }
}
+3

JVM .

, .

.

+2

. - . ...

+1

If you define the subclass class itself:

public class Test {

    public static void main(String[] args) {
        // whatever...
    }

    class TestChild extends Test {
        // whatever...
    }
}

Then yes, it is possible. But note that these are two completely separate classes, except that one is internal to the other.

0
source

Source: https://habr.com/ru/post/1707940/


All Articles