Java super Keyword:

  • The super keyword in java is a reference variable that used to refer to the instant parent class object.
  • Whenever you make an instance of a subclass, an instance of the parent class is built in that i.e. referred to by the super reference variable.

 

java super keyword usage:

  • super can be used to refer to the instant parent class instance variable.
  • super can be used to call the instant parent class method.
  • super () can be used to invite the parent parent constructor immediately.

 

Java super Keyword with variables:

  • This scenario occurs when there are data members similar to the derived class and base class.
  • In that case there is a possibility of ambiguity for JVM.

 

Java super Keyword with methods:

  • This is used when we want to call parent class method.
  • So whenever parents and children have the same designated methods, we use super keywords to solve the ambiguity.

 

Java super Keyword with the Consultants:

  • Super Keywords can also be used for the original Class Constructor.
  • Another important thing is that, on the basis of “super” position, both the parametric and the non-parametric constructor can call.

 

Example:  program without using super keyword:

Output:

 

Example:  program using super keyword:

  • In the above example, both the employee and the hr class have a general property salary.
  • Example variable of the current class is referred to by default by example, but to refer to the parent class frequency variable, we use the super keyword to differentiate between the parent class instance frequency variable and the current class instance variable.

Output: