The super keyword

The super keyword in Java is a reference variable that is used to refer to the immediate parent class object. Whenever you create the instance of the subclass, an instance of the parent class is created implicitly which is referred by super reference variable. The super() is called in the class constructor implicitly by the compiler if there is no super or this.

There are the following uses of super keyword.

  1. super can be used to refer to the immediate parent class instance variable.
  2. super can be used to invoke the immediate parent class method.
  3. super() can be used to invoke immediate parent class constructor.

There are the following differences between this and super keyword.

The super keyword always points to the parent class contexts whereas this keyword always points to the current class context.
The super keyword is primarily used for initializing the base class variables within the derived class constructor whereas this keyword primarily used to differentiate between local and instance variables when passed in the class constructor.
The super and this must be the first statement inside constructor otherwise the compiler will throw an error.
Posted on by