Use of Super Keyword in Java
Ok today we will try to discuss the super keyword of Java. What is the super keyword in Java?, What is the use of super Keyword in Java?, How to practically implement super keyword in Java?, we will discuss all this things. So first note down these important points regarding super keyword in Java.
- Super keyword is used to call immediate parent.
- Super keyword can be used with instance members i.e., instance variables and instance methods.
- Super keyword can be used within constructor to call the constructor of parent class.
OK now let’s practically implement this points of super keyword.
Check out the difference between program 1 and 2. Here program 2 proofs our firstĀ statement of Super keyword in Java.
Program 1
class base
{
int a = 100;
}class sup1 extends base
{
int a = 200;
void show()
{
System.out.println(a);
System.out.println(a);
}
public static void main(String[] args)
{
new sup1().show();
}
}
Output : -
200
200
Now check out the program 2 and try to figure out the main difference.
Program 2
class base
{
int a = 100;
}class sup2 extends base
{
int a = 200;
void show()
{
System.out.println(super.a);
System.out.println(a);
}
public static void main(String[] args)
{
new sup2().show();
}
}
Output
100
200
In the program 1, the output was only of the derived class. It couldn’t print the variable of base class or parent class. But in the program 2, we used a super keyword with the variable a while printing its output and instead of printing the value of variable a of derived, it printed the value of variable a of base class. So it proofs that Super keyword is used to call immediate parent.
OK now check out the difference between program 3 and program 4
Program 3
class base
{
int a = 100;
void show()
{
System.out.println(a);
}
}class sup3 extends base
{
int a = 200;
void show()
{
System.out.println(a);
}
public static void main(String[] args)
{
new sup3().show();
}
}
Output
200
Here the output is 200. When we called the show function, the show function of derived class was called. But what should we do if we want to call the show function of the parent class. Check out the program 4 for solution.
Program 4
class base
{
int a = 100;
void show()
{
System.out.println(a);
}
}class sup4 extends base
{
int a = 200;
void show()
{
super.show();
System.out.println(a);
}
public static void main(String[] args)
{
new sup4().show();
}
}
Output
100
200
Here we are getting two output 100 and 200. When the show function of derived class is invoke, it first calls the show function of parent class because inside the show function of derived class we called the show function of parent class by putting the super keyword before the function name.
Ok here’s a program for you. You need to predict the output of this program
class base
{
int a=100;
void show()
{
System.out.println(a);
}
}class sup5 extends base
{
int a=200;
void show()
{
System.out.println(a);
super.show();
}
public static void main(String[] args)
{
new sup5().show();
}
}
So tell us, what will be the output? So now I think you too know what is super keyword in Java? How and where the super keyword is used in Java? So friends, if you find or face any difficulty in learning this super keyword of Java then let me know.
Share this Post[?]





This won’t work the super keyword has to be writteh in the first line of show()