What are the uses of super keyword in java?


The super keyword in Java is a reference to the object of the parent/superclass. Using it, you can refer/call a field, a method or, a constructor of the immediate superclass.

Referring to a field using super keyword

As mentioned above, you can refer to an instance filed/variable of the immediate superclass from an instance method or, a constructor of a subclass, using the "super" keyword.

If a class extends another class, a copy of members of the parent class will be created in the subclass i.e. Using the object of the subclass you can access the members of both subclass and the superclass.

If a class has a variable with name same as the variable of the superclass then,  you can differentiate the superclass variable from the subclass variable using the super keyword.

Example

In the following Java example, we have two classes SuperClass and SubClass here, the SubClass extends the SuperClass and, both have the same variable "name".

From a method of the class SubClass (display()), we are printing the value of the instance variable (name) of both  classes using "super" and "this" keywords respectively −

 Live Demo

class SuperClass{
   String name = "Raju";
   public void display() {
      System.out.println("This is a method of the superclass");
   }
}
public class SubClass extends SuperClass{
   String name = "Ramu";
   public void display() {
      System.out.println("This is a method of the superclass");
      System.out.println("Value of name in superclass: "+super.name);
      System.out.println("Value of name in subclass: "+this.name);
   }
   public static void main(String args[]) {
      new SubClass().display();
   }
}

Output

This is a method of the superclass
Value of name in superclass: Raju
Value of name in subclass: Ramu

Referring to a superclass constructor using super keyword

You can also refer/call a constructor of the superclass from the constructor of the child/subclass using the "super" keyword (explicitly).

Example

In the following Java example, we have two classes SuperClass and SubClass. Here, the SubClass extends the SuperClass and both classes have default constructors. We are trying to call the constructor of the superclass from the constructor of the subclass using the super keyword −

 Live Demo

class SuperClass{
   String name = "Raju";
   public SuperClass() {
      System.out.println("This is the constructor of the superclass");
   }
}
public class SubClass extends SuperClass{
   String name = "Ramu";
   public SubClass() {
      System.out.println("This is a constructor of the subclass");
   }
   public static void main(String args[]) {
      new SubClass();
   }
}

Output

This is the constructor of the superclass
This is a constructor of the subclass

Calling a method using this keyword

You can also use this to call an (instance) method of the superclass from the instance method of the subclass.

Example

In the following Java example, we have two classes SuperClass and SubClass. Here, the SubClass extends the SuperClass.

Here, we are trying to invoke the display() method of the superclass using the super keyword from the subclass’s method named show.

 Live Demo

class SuperClass{
   String name = "Raju";
   public void display() {
      System.out.println("This is a method of the superclass");
   }
}
public class SubClass extends SuperClass{
   String name = "Ramu";
   public void show() {
      System.out.println("This is a method of the subclass");
      super.display();
   }
   public static void main(String args[]) {
      new SubClass().show();
   }
}

Output

This is the constructor of the superclass
This is a constructor of the subclass

Updated on: 29-Jun-2020

782 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements