Java : Class inheriting self

后端 未结 8 1317
刺人心
刺人心 2021-02-15 11:19

I know this is pointless: I just find it funny and I want to inquire more about the mechanics of what happens when you create a class that inherits itself, resulting in a stack

相关标签:
8条回答
  • 2021-02-15 11:28

    When I try to compile :

    class A extends A {
    }
    

    I get :

    $ javac A.java
    A.java:1: cyclic inheritance involving A
    class A extends A {
    ^
    1 error
    

    So Java don't let you do this kind of thing. For information, java version "1.6.0_24"

    0 讨论(0)
  • 2021-02-15 11:37

    Try in an IDE like eclipse, it wont allow you to do so. ie gives an error like this.

    Cycle detected: the type Test cannot extend/implement itself or one of its own member types

    0 讨论(0)
  • 2021-02-15 11:39

    The java compiler is not going to enter into an infinite loop when trying to enter a cyclic inheritance chain. After all, every inheritance chain is a eventually finite graph (and, computationally speaking, with a very small number of nodes and edges.) More precisely, the inheritance graph from subclass A to (eventual) superclass Z must be a line (not the other way around, though), and the compiler can easily determine if it is a line or not.

    It does not take much for a program to determine if such a small graph is cyclic or not, or if it is a line or not, which is what the compiler does. So the compiler does not go into an infinite loop, and the JVM never runs out of stack space since 1) neither the compiler runs on the JVM, nor 2) the JVM get to executes (since nothing gets to compile and the compiler never invokes under such conditions the JVM anyways.)

    I'm not aware of any languages that permit such cyclic inheritance graphs (but I've been doing nothing but Java for 11 years, so my memory of anything other than Java is mushy.) I cannot see, furthermore, the use of such a construct (in modeling or real life). Might be theoretically interesting, though.

    edit

    Ok, I ran your code and indeed it causes an stack overflow. You were right. I'm gonna have to sit and really study this to understand why the compiler allows such a construct.

    Nice find!!!!

    0 讨论(0)
  • 2021-02-15 11:40

    In its final form this problem has nothing to do with cyclic inheritance and inner classes. It's just an infinite recursion caused by unbound recursive constructor call. The same effect can be shown by the following simple example:

    public class A {
        public A() {
            new A();
        }
    }
    

    Note that this code is perfectly valid, since Java doesn't apply any restrictions on recursive calls.

    In your case it's slightly more complicated due to inheritance, but if you recall that constructor of subclass implicitly call a constructor of superclass, it should be clear that these calls form infinite recursion.

    0 讨论(0)
  • 2021-02-15 11:42

    You can get the answer by:

    Class.forName("MyClass");
    

    This way it gets resolved but not instantiated. So you can chack if resolution itself causes the crash.

    I guess it depends on the JVM you use.

    0 讨论(0)
  • 2021-02-15 11:49

    Remember that, since Inside extends Outside, it has an implicit call to super() which is the constructor of Outside (which in turn calls the constructor of Inside) and so it goes around.

    The code you posted is conceptually not different from the following program:

    class A {
        B b = new B();
    }
    
    class B extends A {
    }
    
    public class Test {
        public static void main(String[] args) {
            new A(); // Create an A...
                     //   ... which creates a B
                     //   ... which extends A thus implicitly creates an A
                     //   ... which creates a B
                     //   ...
        }
    }
    
    0 讨论(0)
提交回复
热议问题