super() in Java

前端 未结 15 2043
逝去的感伤
逝去的感伤 2020-11-22 05:36

Is super() used to call the parent constructor? Please explain super().

15条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 05:58

    That is correct. Super is used to call the parent constructor. So suppose you have a code block like so

    class A{
        int n;
        public A(int x){
            n = x;
        }
    }
    
    class B extends A{
        int m;
        public B(int x, int y){
            super(x);
            m = y;
        }
    }
    

    Then you can assign a value to the member variable n.

提交回复
热议问题