Super Keyword in Java

 

In Java programming language the super keyword is considered as a reference variable that is used to refer the object of the parent class. When the instance of the subclass is created an instance of the parent or super class is also created implicitly and this instance is referred by the super reference variable.

 

Usage of Java super keyword:

The Java super keyword is used to refer to the immediate instance variable of the parent or super class. The method super () is used to call or invoke the constructor of the immediate parent class. We can also use the super keyword in Java programming language to call or invoke the method of the immediate parent or super class.

 

Super is used to refer immediate parent class instance variable:

Consider the following example in which we will demonstrate that what will happen if we do not use the super keyword:

CODE:

class Automobile {

int velocity = 50;

}

class Car extends Automobile {

int velocity = 60;

void display () {

system. out. println (velocity);

}

public static void main (string args []) {

Car c = new Car ();

  1. display ();

}

}

OUTPUT:

60

In the above example the parent class that is Automobile and the child class Car both have same property that is velocity. The instance variable of the current class is referred by instance by default. But the instance variables of the parent class should be referred. This is the main reason to use the super keyword in our program that is to distinguish between the instance variable of the parent class and the instance variable of the child class.

Now consider the following example in which we have used the super keyword to overcome the above problem:

CODE:

class Automobile {

int velocity = 50;

}

class Car extends Automobile {

int velocity = 60;

void display () {

system. out. println (super. velocity);

}

public static void main (string args []) {

Car c = new Car ();

  1. display ();

}

}

OUTPUT:

50

In the above example the parent class that is Automobile and the child class Car both have same property that is velocity. The instance variable of the current class is referred by instance by default. But the instance variables of the parent class should be referred. Therefore, we used the super keyword to refer to the instance variable of the parent class.

 

Super keyword is used to invoke the parent class constructor:

In Java programming language we can use the super keyword to call or to invoke the constructor of the parent class, consider the following example:

CODE:

class Animal {

Animal () {

system. out. println (“Animals that are mammals”);

}

}

class Cat extends Animal {

Cat () {

super ();

system. out. println (“Cats are mammals”);

}

public static void main (string args []) {

Cat c1 = new Cat ();

}

OUTPUT:

Animals that are mammals

Cats are mammals

In the above example, we have a parent class that is Animal and the child class that is “Cat”. When the object of the child class is created in the main () method, the constructor of the class “Cat” was called and in its constructor we used super () to invoke or to call the constructor of the parent class. In this way we got the output that includes the message that was in the parent class and in the child class also.

In Java programming language the compiler automatically adds super () in each constructor of the class.

The default constructor in java programming language is automatically added by the compiler and the compiler also adds super () in the constructor as the first statement in the constructor. If the programmer has created his own constructor but the constructor does not have this () and super () then the compiler will add super () in the constructor as the first statement.

 

Another example of super keyword where super () is provided by the compiler implicitly:

Consider the following example in which we did not use super () but used this () and the compiler will add super () implicitly:

CODE:

class Animal {

Animal () {

system. out. println (“Animals that are mammals”);

}

}

class Cat extends Animal {

Cat (int number) {

this. number = number;

system. out. println (“Cats are mammals”);

system. out. println (number);

}

public static void main (string args []) {

Cat c1 = new Cat (3);

}

OUTPUT:

Animals that are mammals

Cats are mammals

3

In the above example, we have a parent class that is Animal and the child class that is “Cat”. When the object of the child class is created in the main () method, the constructor of the class “Cat” was called. And we used the “this” keyword also to refer to instance variable of the class cat. The compiler added the super () implicitly which called the constructor of the parent class and the message was displayed which was in the constructor using print statement.

In Java programming language the compiler automatically adds super () in each constructor of the class.


Super can be used to invoke parent class method:

In java programming language the super keyword can also be used to call or to invoke the method of the parent class. We use the super keyword in java programming language to invoke or call the method when the subclass and the parent class has same method that is when we are overriding the methods of the parent and child class.

Consider the following example in which we used the super keyword to invoke the method of the class:

CODE:

Class Person {

Void display () {

System.out. println (“Hello”);

}

}

class Employee extends Person {

void display () {

system. out. println (“Hello World”);

void printing () {

display ();

super. display ();

}

public static void main (string args []) {

Employee e = new Employee ();

  1. display ();

}

}

OUTPUT:

Hello

Hello World

In the above example, the parent class that is the Person class and the child class that is the Employee class both have same method named display (). In the method printing (), the first statement that is display () is used to invoke or call the display () method of the current class and the second statement that is super. display () is used to call or invoke the display () method of the parent or super class.

As both of the above classes that are person the parent class and employee the child class has the same method that is display (). Therefore, when we call the display () method from the employee class then the display () method of employee class will be called because priority will be given to local.

Program in case super is not required:

Consider the following code in which there is no need of using the super keyword:

CODE:

class Person {

void display () {

system. out. println (“Hello”);

}

}

class Employee extends Person {

void printing () {

display ();

}

public static void main (string args []) {

Employee e = new Employee ();

  1. display ();

}

OUTPUT:

Hello

In the above example, there is no display () method in the child class and it is only in the parent class that is in the Person class. As the method is only in the parent class therefore, the super keyword to call or invoke the method of the parent class is not required.