SUPER KEYWORD IN JAVA

Super keyword in Java is a reference variable that is used to refer to the parent class object. It is able to represent super class object from sub-classes. Would recommened to first visit Inheritance in Java before starting with super keyword module.

Table of Contents

[hide]
  • What is Super Keyword in Java
  • Refer Super or Parent class Instance variable
  • Invoke Super or Parent class method
  • Invoke Super or Parent class Constructor

What is Super Keyword in Java

Super Keyword in Java is similar to this keyword in java with some changes in how they access the Instance variables Instance variables, methods and constructors . this keyword in Java is used to refer to the current class Instance, Current class method,current class constructor while super keyword in java is used to refer to the parent class Instance, class methods and class constructors.

USECASE :If Parent and Child class contains the same variable name or method name then we use either this keyword or super keyword to distinguish between the current class and Parent class methods and variables.

Restriction : You can use this keyword and super keyword anywhere in Java Program except the static area else you would get the compile time error. We can use them any number of times in our program.

Syntax : Super keyword

super() 

Three uses of the Java Super Keyword are :

  • 1. Used to Refer Super or Parent class instance variable from sub-class.
  • 2. Used to Invoke Super or Parent class method from sub-class.
  • 3. Used to Invoke Super or Parent class Constructor from sub-class.

Let us learn about these usages in detail

Refer Super or Parent class Instance variable

Let us consider a scenario where we have a variable that is present in both the parent class and child class and we want to acces the parent class variable from the child class, then we will make the use of super keyword.

Example 1 : Super Keyword in Java

copyimage
//Java Program to demonstrate the use of Super Keyword in Java
class SuperClass { 
    String name="James";
}

class SubClass extends SuperClass  { 
    String name="Max";
    public void show() {
      System.out.println("Instance variabe value : " + super.name);
  }
}
public class Codebator{
  public static void main(String[] args) {
  	SubClass obj=new SubClass();
  	obj.show();
  }
}

OUTPUT :

Instance variabe value : James

As you can see both the classes have the common property name. If we print the name property without using super, then it will print the name of current class. Super class let us access the parent class variabe as seen in the efxample.

Invoke Super or Parent class method

If any time parent and child class have the same property method, we use super keyword to distinguish between the two. Let us see an example below which will help us to understand the concept easily.

Example 2 : Super Keyword in Java

copyimage
//Java Program to demonstrate the use of Super Keyword in Java
class Student { 
    String name = "Virat Kohli";
    public void studentInfo() {
    System.out.println("This is Student Class");
 }
}
class GraduateStudent extends Student  { 
    String hobby = "Cricket";
    public void studentInfo() {
    System.out.println("This is Graduate Student Class");
}
  void display() {
    studentInfo(); // invoke current class method
    super.studentInfo(); //invoke Parent class method
  }
}
public class Codebator{
public static void main(String[] args) {
    GraduateStudent obj = new GraduateStudent();
    obj.display();
  }
}

OUTPUT :

This is Graduate Student ClassThis is Student Class 

In the above example you can see that when we call studentInfo() from child class the current class method is being invoked, but as soon as we use super keyword with studentInfo then you can see that it access the parent class method.

Invoke Super or Parent class Constructor

We can also use Super keyword to access parent class constructor. Depending upon the situation we can call parameterized or non parameterized constructor. Let us see an example to understand the concept in detail.

Example 3 : Super Keyword in Java

copyimage
//Java Program to demonstrate the use of Super Keyword in Java
class Student {
   Student() {
      System.out.println("This is Student Class");
   }
}

class GraduateStudent extends Student {
GraduateStudent() {
   super();
      System.out.println("This is Graduate Student Class");
   }
}

public class Codebator {
   public static void main(String[] args) {
      GraduateStudent obj = new GraduateStudent();
   }
}

OUTPUT :

This is Student ClassThis is Graduate Student Class 

Note: Even if you dont put super keyword in the current class constructor then also compiler will call the super since super is added in each class constructor automatically.

Example 4 : Super Keyword in Java

copyimage
//Java Program to demonstrate the use of Super Keyword in Java
class Student {
   Student() {
      System.out.println("This is Student Class");
   }
}

class GraduateStudent extends Student {
GraduateStudent() {
      System.out.println("This is Graduate Student Class");
   }
}

public class Codebator {
   public static void main(String[] args) {
      GraduateStudent obj = new GraduateStudent();
   }
}

OUTPUT :

This is Student ClassThis is Graduate Student Class 

You can see that we havnt called super keyword but still the output is same, this indicated that super is added at each class constructor automatically by the compiler .

Let see one more example of super keyword with this keyword

Example 5 : Super Keyword in Java

copyimage
//Java Program to demonstrate the use of Super Keyword in Java
class Vehicle {
   int speed;
   String name;

   Vehicle(int speed, String name) {
      this.speed = speed;
      this.name = name;
   }
}

class Car extends Vehicle {
String brand;

   Car(int speed, String name, String brand) {
      super(speed, name);// reusing parent constructor
      this.brand = brand;
   }

   void show() {
      System.out.println(speed);
      System.out.println(name);
      System.out.println(brand);
   }
}

public class Codebator {
   public static void main(String[] args) {
      Car obj = new Car(200, "BMW 7 Series", "BMW Group");
      obj.show();
   }
}

OUTPUT :

200BMW 7 Series BMW Group 

In this module we have learned the use of Super Keyword. Better knowledge of Super keywprd and when to use them will enable us to improve our programming skills to a large extent.