super, super()

reference type variable super, constructor super()

  • super

  1. reference type variable that indicates itself.

  2. exist within instance method(constructor).

  3. distinguish parent members between current members

class Parent {int x = 10;}

class Child extens Parent {
    int x = 20;
    
    void omethod() {
        System.out.prinln("x=" - x);
        System.out.prinln("this.x=" + this.x);
        System.out.prinln("super.x=" + super.x);
    }
}
  • super() : parent constructor

  1. call parent constructor

  2. initialize parent member by calling parent constructor

  3. add constructor in the first line otherwise compiler adds super(); automatically. 모든 생성자는 첫 줄에 다른 생성자를 호출해야한다. 그렇지 않으면 컴파일러가 자동으로 super()를 호출해버린다.

right code below :

  1. default constructor in Point class

  2. must call other constructor in first line

  3. super constructor in Point3D constructor

Last updated

Was this helpful?