Java- 'super' keyword in java with example

The super keyword in Java is a reference variable which is used to refer to immediate parent class object.
Whenever you create the instance of a subclass, an instance of the parent class is created implicitly which is referred to by a super reference variable.

The use of super keyword

1) To access the data members of parent class when both parent and child class have a member with the same name
2) To explicitly call the no-arg and parameterized constructor of the parent class
3) To access the method of parent class when child class has overridden that method.


To access the data members of parent class when both parent and child class have a member with the same name

We can use the super keyword to access the data member or field of the parent class. It is used if parent class and child class have the same fields.



class Animal {
String action = "Animal sleeping";
}

public class Lion extends Animal {
String action = "Lion roaring";

public void action() {
System.out.println(action);
System.out.println(super.action);
}

public static void main(String[] args) {
Lion obj = new Lion();
obj.action();
}
}

Output:
Lion roaring
Animal sleeping


To explicitly call the no-arg and parameterized constructor of the parent class

When we create the object of the subclass, the new keyword invokes the constructor of the child class, which implicitly invokes the constructor of the parent class. So the order to execution when we create the object of child class is: parent class constructor is executed first and then the child class constructor is executed. It happens because the compiler itself adds super()(this invokes the no-arg constructor of parent class) as the first statement in the constructor of the child class.
Let’s see an example to understand what I have explained above:

class Animal {
Animal() {
System.out.println("Animal sleeping");
}
}

public class Lion extends Animal {

Lion() {
/*
* Compile implicitly adds super() here as the first statement of this
* constructor.
*/
System.out.println("Lion roaring");
}

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

Output:
Animal sleeping
Lion roaring

 To access the method of parent class when child class has overridden that method.

The super keyword can also be used to invoke the parent class method. It should be used if the subclass contains the same method as the parent class. In other words, it is used if the method is overridden.

class Animal {

void sleeping() {

System.out.println("Animal sleeping");

}

}

public class Lion extends Animal {
void sleeping() {
super.sleeping();
System.out.println("Lion roaring");
}

public static void main(String[] args) {
Lion obj = new Lion();
obj.sleeping();
}
}

Output:
Animal sleeping
Lion roaring



Comments

  1. Genuine and Free jobs!
    Don't miss any job opportunities!
    Improve your life, only one step!

    More Java jobs info, Please join the Java Jobs group:
    https://chat.whatsapp.com/HLFm93tkHQR4zu8hafpqSl

    ReplyDelete

Post a Comment