Java Super Keyword

In Java, the super keyword is a reference variable that is used to refer to the object of the immediate parent class.

When you create a subclass instance, an implicit instance of the parent class is also created and is referred to by the super reference variable.

The super keyword is used to

  • Access the variable of the parent class instance.
  • Invoke the method of the parent class.
  • Invoke the constructor of the parent class.

Access the variable of the parent class instance

We can use the super keyword to access and use the parent class variable.

Example to access parent class variable with super keyword

    class Animal {
      String color = "black";
    }
    class Cat extends Animal {
      String color = "white";
      void PrintCatColor() {
        System.out.println("Cat Color: "+color);
        System.out.println("Animal Color: "+super.color);
      }
    }
    class Main {
      public static void main(String args[]) {
        Cat cat = Cat Dog();
        cat.PrintCatColor(); // Dog Color: white
// Animal Color: black
      }
    }

The above program has a class Animal with a variable color with default value black. The Animal class is inherited by the Cat class which also has the variable color with default value white.

The PrintCatColor() method will print the value in color variable of Cat class and to print the Animal class variable value of color we use super.color.

When we create an object for the Cat class and call the PrintCatColor() method we get the following output

Cat Color: white
Animal Color: black

Invoke the method of the parent class

Using the super keyword we can access the method of the parent class.

Example for invoking the method of parent class

class Animal {
  public void Category() {
    System.out.println("I'm an animal");
  }
}

class Cat extends Animal {
  public void Type() {
    System.out.println("I am a cat.");
  }
  public void Behavior() {
    super.Category();
    Type();
  }
}

class Main {
  public static void main(String[] args) {
    Cat cat = new Cat();
    cat.Behavior();
  }
}

In the above program, the Animal class has a method Category() and this Animal class is inherited by the Cat class.

The Cat class has 2 methods Type() and Behavior().
In the Behavior() method we call the Category() method of the Animal class using super.Category() and Type() method.

Next, we create an object for the Cat class and call the Behavior() function. It will display the following output

I'm an animal
I am a cat.

Invoke constructor of parent class

Sometimes we need to invoke the constructor of the parent class. In such situation we use super() method to call the immediate parent class constructor.

class Animal {
  // Animal class constructor
  Animal() {
    System.out.println("Animal Category");
  }
}

class Cat extends Animal {

  Cat() {
    super(); // Call the Animal(parent) class constructor
    System.out.println("I am a Cat");
  }
}

class Main {
  public static void main(String[] args) {
    Cat cat = new Cat();
  }
}

The above program is an example for calling the constructor of the parent class.

Here, the Animal class has a constructor and this class is inherited by the Cat class.

The Cat class contains a constructor. Inside the constructor we will call the constructor of the Animal class using super() method.

Now when we create an object for the Cat class, the output will be,

Animal Category
I am a Cat

Real-world example where the super keyword is used

class Employee {
  String empId;
  String name;
  Employee(String name, String empId) {
    this.empId = empId;
    this.name = name;
  }
}
class Developer extends Employee {
  float salary;
  Developer(String empId, String name, float salary) {
    super(empId, name); // Call the Employee Constructor
    this.salary = salary;
  }
  void Display() {
    System.out.println("Employee Id: " + empId);
    System.out.println("Employee Name: " + name);
    System.out.println("Employee Salary: " + salary);

  }
}
class Main {
  public static void main(String[] args) {
    Developer emp = new Developer("Emp001", "Andrew", 60000 f);
    emp.Display();
  }
}

In the above program, we have created a class Employee to have the common details of employee of any category. Here it contains the empId and name variable.

Next, we create a class Developer which inherits the Employee class. The Developer class has a parameterized constructor with empId, name and salary.

The Developer class constructor, then calls the super() method and invokes the Employee class constructor to initialize the value of empId and name variable.

Later, in the Main class we create an object for the Developer class with the parameters and call the Display() method of the class to display the details of the developer as below.

Employee Id: Andrew
Employee Name: Emp001
Employee Salary: 60000.0

Most Read