The super keyword in Java is a perfect connection variable used to relate to the properties of the parent process. With the notion of inheritance in Java, the keyword “super” emerged into the scenario. It is being utilized frequently in the following perspectives:
- In variable phase
- Methodology phase
- In constructor phase
Table of Contents
Use of super keyword in Java
The java super keyword can be utilized in the mentioned ways:
- Enable parent class data elements as both parent and child classes have participants of the very same names
- To directly call the no-argument and parent class parameterized constructor
- To control the parent class process when that process has been overridden by a child class
Thus, the super keyword in java is used to resolve the misunderstanding that develops among the subclasses and superclasses.
Unique phases when a super keyword is utilized
At different phases, the super keyword in java is utilized individually as per the needs. Those different ways are mentioned below with their respective illustrations. Let’s have a grasp on them!
1. In variable phase:
When any derived class inherits participants of the base data points, there is indeed a risk that the participant of the base class data is equivalent to the participant of the derived class information and JVM receives an uncertainty.
To distinguish between both the base class data member and the derived class, the base class data member must always be followed by a super keyword in the sense of the derived class.
// Let’s make the parent class
class demo_bird{
String d_col= “Blue”;
}
// Let’s make the child class
class demo_sparrow extends demo_bird{
String d_col= “Brown”;
void dis_col(){
// This below line of code displays the color of a child or demo_sparrow class
System.out.println(d_col);
// This below line of code displays the color of a parent or demo_bird class
System.out.println(super.d_col);
}
}
class demo_super1{
public static void main (String args[]){
demo_sparrow sw1= new demo_sparrow();
sw1.dis_col();
}
}
// Output of the illustration will be:
// Brown
// Blue
2. In the methodology phase:
You may also use the super keyword to initiate or call the parent class function. It is being utilized in some events of an overriding process. Using the super keyword in another phrase is when the identity of the base class process as well as the derived class process has the same name.
// Let’s make the parent class
class demo_girl1{
void msg_1(){
System.out.println(“Welcome to Java world! I am a girl instructor.”);
}
}
//Let’s make the child class
class demo_boy1 extends demo_girl1{
void msg_1(){
System.out.pritnln(“Welcome to Java world! I am a boy instructor.”);
}
void dis_msg(){
// This below line of code will display the message of demo_boy1 class
msg_1();
// This below line of code will display the message of demo_girl1 class
super.msg_1();
}
}
class demo_super2{
public static void main (String args[]){
demo_boy1 db1= new demo_boy1();
db1.dis_msg();
}
}
// Output of the illustration will be:
// Welcome to Java world! I am a boy instructor.
// Welcome to Java world! I am a girl instructor.
3. In the constructor phase:
The super keyword in java could also be used to activate the constructor of the parent class. A further critical aspect is that, depending on the context,” super ‘can name both parametric and non-parametric constructors. The constructors are called from starting at the bottom to reaching the top and they are implemented from starting at the top to reaching the bottom. The code snippet to illustrate the above description is below:
// Let’s declare the parent or base class that is house_demo1
class house_demo1{
house_demo1(){
System.out.println(“Hello! I am the constructor of house!”);
}
}
// Let’s declare the child class flats_demo1 which inherits from the parent class
class flats_demo1 extends house_demo1{
flats_demo1(){
// This below code calls the constructor of a parent or base class that is house_demo1
super();
System.out.println(“Hello! I am the constructor of flats!”);
}
}
class demo_const_1{
public static void main(String args[]){
flats_demo1 fd1= new flats_demo1();
}
}
// Output of the illustration will be:
// Hello! I am the constructor of house!
// Hello! I am the constructor of flats!
this() v/s super() in Java
this() in Java | super() in Java |
It refers to the actual instance of the class. | It relates to an absolute iteration of a parent class. |
This() is being utilized to activate the actual class procedure. | It is being utilized to activate the parent class procedure instantaneously. |
This() serves as the present constructor of classes and should be utilized in the constructors that are parametrized. | Super() serves as the constructor of the specific parent class and must be the first line in the constructor of the child class |
This keyword can be used when activating an existing version of an overridden procedure. | The super keyword should be used when activating a superclass variant of an overridden procedure. |