Super Keyword in Java: The Keyword Super refers to the immediate parent class object. But before we are going to learn about What is a super keyword and how to use the super keyword in Java programming language, we know inheritance. If you don’t have any idea about inheritance, then you can follow our Inheritance Java tutorial guide here.
Use of Super Keyword
- You Can access the data member of the parent class using the super keyword when the same data member present in both parent and child class.
- You Can call the parent class no-arg constructor or parametrized constructor using the Super keyword in child class.
- When you have to override a parent class method in the child class, but you want to call the parent class method, that time, you need to use the super keyword for access to the parent class method.
Let’s discuss how to use super keyword with variables, methods & constructors with simple examples so that we can understand them easily.
How to Use super Keyword With Variables
When in your program, both parent and child class have the same variable or data member. In this case, there is a chance of ambiguity for JVM. So to access the parent class variable from the child class, we can use the super keyword. Let’s go through with a program so that you can understand how the super keyword and how it is working with the variables.
package com.SoftwareTestingo.JavaBasics; class Parent { //Super Class Variable int var=111; } public class Super_Keyword_With_Variable extends Parent { //Child Class Variable int var=222; public void printSuperVar() { System.out.println("Parent Class Var Value: "+super.var); } public static void main(String[] args) { Super_Keyword_With_Variable obj=new Super_Keyword_With_Variable(); System.out.println("The Value Of Local Variable: " + obj.var); obj.printSuperVar(); } }
Output:
The Value Of Local Variable: 222 Parent Class Var Value: 111
How to Use super keyword with Method
When you want to call the parent class method from the child class, where the parent and child class both have the same method name, this type of situation is called method overriding. But there is a chance of ambiguity for the JVM because in child class it has both parent and child class method with the same name, so to resolve the ambiguity situation we can use the super keyword.
Go through with the below example program to understand clearly without any confusion
package com.SoftwareTestingo.JavaBasics; class PClass { void show() { System.out.println("Parent Class Show Method Called"); } } public class Super_Keyword_With_Method extends PClass { void show() { System.out.println("Child Class Show Method Called"); } void callShowMethod() { show(); super.show(); } public static void main(String[] args) { Super_Keyword_With_Method obj=new Super_Keyword_With_Method(); obj.callShowMethod(); } }
Output:
Child Class Show Method Called Parent Class Show Method Called
Note: When the child class is not overriding the parent class methods, that time no need to use the super keyword to access the parent class methods.
How to Use Super keyword with Constructor
When we are creating the object of child class using the new keyword, that time it calls the child class default constructor. The child class constructor indirectly calls the parent class constructor. So The order of execution of construct is from parent class to child class.
It all happens because the JVM compiler automatically adds the super() statement at the beginning of the child class construct. So the super() statement calls the no-args constructor of the parent class. In this way, the constructor execution comes from the parent class constructor to the child class constructor.
Let’s go through with an example to understand how the child class constructor calls the parent class constructor.
package com.SoftwareTestingo.JavaBasics; class ParentClass { public ParentClass() { System.out.println("Parent Class No Arg Consructor Executed"); } public ParentClass(String abc) { System.out.println("Parent Class With Argument Consructor Executed"); } } public class Super_Keyword_With_Constructor extends ParentClass { public Super_Keyword_With_Constructor() { //super(); super("Welcome"); System.out.println("Child Class Constructor Got Executed"); } public static void main(String[] args) { Super_Keyword_With_Constructor obj=new Super_Keyword_With_Constructor(); System.out.println("Main Method executed"); } }
Output:
Parent Class With Argument Consructor Executed Child Class Constructor Got Executed Main Method executed
How to Call the Parent Class parametrized Constructor from Child Class
But If our requirement is called the parent class parameterized constructor, we can also achieve that by using the parametrized super() statement inside the child class constructor.
Go through with the below Example program:
package com.SoftwareTestingo.JavaBasics; class Super_Parent_Class { Super_Parent_Class() { System.out.println("Parent Class Default Constructor"); } Super_Parent_Class(String str) { System.out.println(str +": This is Parametrized Constructor Of Parent Class"); } } public class Super_Keyword_With_Param_Constructor extends Super_Parent_Class { Super_Keyword_With_Param_Constructor() { super("User"); System.out.println("Child Class Default Constructor"); } public void display() { System.out.println("Simply Printing Hello"); } public static void main(String[] args) { Super_Keyword_With_Param_Constructor obj=new Super_Keyword_With_Param_Constructor(); obj.display(); } }
Output:
User: This is Parametrized Constructor Of Parent Class Child Class Default Constructor Simply Printing Hello
Note: The Super() should be the first statement inside the method where you want to call the parent constructor.
Important Point About Super() In Constructor
- When you are using a super() statement in the child class constructor, that time the super() statement should be the first statement of the child class. Otherwise, you will get the compilation error: “Constructor call must be the first statement in a constructor”.
- If you have not added any super() statement inside the child class constructor that time Java JVM compiler automatically add super() statement at the beginning of child constructor, if the parent class doesn’t have any no-argument constructor that time, you will get a compile-time error.
- When you mention the super() statement inside the child class constructor that time the JVM compiler doesn’t call the parent class no-args constructor.
If you found any mistake or you want to add some more information in this post, then you can comment in the comment section.
Leave a Reply