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

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

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

相关标签:
3条回答
  • 2021-01-29 15:42

    super(args...) is required for the constructor's at first line.

    super.property can be used anywhere in the code

    0 讨论(0)
  • 2021-01-29 15:42

    Then you might be making a method call and not Sayin super() inside constructor.. for instance..

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

    The above code works fine...

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题