Why does the super() not showing error though not given in first line?

前端 未结 3 638
栀梦
栀梦 2021-01-29 15:14

Why does the super keyword not showing error though not given in first line

3条回答
  •  一整个雨季
    2021-01-29 15:47

    It does for me:

    class Superclass
    {
    }
    
    class Subclass extends Superclass
    {
        Subclass()
        {
            System.out.println();
            super();
        }
    }
    

    Compiling gives an error of:

    Test.java:10: call to super must be first statement in constructor
            super();
                 ^
    1 error
    

    Please show a similar short but complete program with it not giving an error. Note that I'm assuming you really have got super(); rather than, say, super.foo(); which is just a call to the superclass implementation of foo(); and can appear anywhere in a method or constructor.

提交回复
热议问题