Super Keyword in Java


JavaViews 3648

Super Keyword in Java is a reference variable that we use in inheritance to refer to the immediate parent class object from the child class. In this tutorial, we will discuss in detail about the different uses of the java super keyword.

It is important that you must have knowledge of inheritance and polymorphism before understanding the super keyword

Uses of Super Keyword in Java

  • Access the parent class variable
  • To invoke the parent class method
  • To invoke the parent class constructors with and without argument.

Java super keyword

Java super keyword to access parent class variable

When both the parent and the derived classes have the same variable name, we can use the java super keyword to access the parent class variable.

Syntax

super.variable_name;

Example

In the below example, we have the same String variable role in both the parent class Employee and the child class Developer. To access the variable name of the child class, we can directly use the variable name. To access the variable of the parent class, we use along with the java super keyword.

class Employee {
  String role = "Software Engineer";
}
public class Developer extends Employee {

  String role = "Developer";
  
  public void displayRole() {
    System.out.println(role);
    System.out.println(super.role);
  }
  public static void main(String[] args) {
    Developer d = new Developer();
    d.displayRole();

  }

}
Developer
Software Engineer

Using Super Keyword to invoke a parent class method

When both the parent and child class have the same method name, then we use the java super keyword in the child class to access the parent class method. This concept is also called a method overriding. You can refer to polymorphism in java to know more about this concept.

Syntax

super.method_name();

Example

In the below example, we have the same method name displayRole in both the parent class Employee and the child class Developer. We can directly call the method name of the child class using the child class object from the main method or direct call within another method. But to access the parent class method from the child class, we need to use the super keyword i.e super.displayRole().

class Employee {
  String role = "Software Engineer";
  
  public void displayRole() {
    System.out.println("Employee Role: " + role);
  }
  
}
public class Developer extends Employee {

  String role = "Developer";
  
  public void displayRole() {
    System.out.println(role);
    super.displayRole();
  }
  public static void main(String[] args) {
    Developer d = new Developer();
    d.displayRole();

  }

}
Developer
Employee Role: Software Engineer

Suppose, the child class does not override the parent class method, then we can directly call the parent method without using the java super keyword.

class Employee {
  String role = "Software Engineer";
  
  public void displayRole() {
    System.out.println("Employee Role: " + role);
  }
  
}
public class Developer extends Employee {
  
  public void show() {
    displayRole();
  }
  
  public static void main(String[] args) {
    Developer d = new Developer();
    d.show();

  }

}
Employee Role: Software Engineer

Using Super Keyword to invoke a constructor

When we create an object for the child class using the new keyword, it calls the constructor of the child class. The java compiler in turn implicitly calls the default no parent class constructor ( no argument) using the super keyword. In this way, it first executes the parent class constructor, and then the child class constructor. It is not mandatory to explicitly call the parent constructor using the java super keyword if the constructor does not have any argument.

Syntax

super();

Example: Default constructor

In the below example, we have a parent class constructor Shapes() which does not have any argument. In this case, when we create an object for the class Square, then it calls the child constructor Square(). The JVM internally calls the parent class constructor Shapes() using the super(). This is the reason that it first executes the Shapes constructor and then followed by the Square constructor.

Java super keyword

class Shapes {
  Shapes() {
    System.out.println("This is a shape");
  }
}

public class Square extends Shapes {
  
  Square() {
  System.out.println("This is a square");	
  }

  public static void main(String[] args) {
    Square s = new Square();
    

  }

}
This is a shape
This is a square

Example: Parameterized constructor

Suppose, the parent class has a constructor with parameters, then we need to explicitly call it using the super keyword. The JVM does not make an implicit call for parameterized constructors. It is always important to note that the super statement should be the first statement in the child constructor. Let’s understand this with an example.

Syntax

super(parameter);

Example

In the below example, when we create an instance of Square class, it calls the Square() constructor. Here, we call the parent class constructor Shapes(string name) using the super keyword by passing the parameter. Hence it executes this statement first after which it executes the statements inside the Square() constructor.

Since we explicitly call the parameterized parent class constructor using the super keyword, the JVM does not make another implicit call to the default parent constructor. This is why it did not execute the Shapes() constructor.

class Shapes {
  Shapes() {
    System.out.println("This is a shape");
  }
  
  Shapes(String name) {
    System.out.println("Shape name is: " + name);
  }
}

public class Square extends Shapes {
  
  Square() {
    super("Square");
    System.out.println("Square constructor");
  }

  public static void main(String[] args) {
    Square s = new Square();

  }

}
Shape name is: Square
Square constructor

Reference

Translate »