super Keyword in java

super Keyword in java is used to refer the object of the immediate superclass, and it is related to inheritance in java.

Let’s say you have an instance variable or method of same name in subclass and superclass.

How will JVM know which one are you referring to; superclass or subclass?
That’s where you can use super keyword to refer superclass’s variables, methods or constructor.

Let’s understand this with the help of examples.

super keyword in java can be used at three-levels.

  1. Usage of super at variable level
  2. Usage of super at Constructor level
  3. Usage of super keyword at method level

Usage of super at variable level

We can refer to variable of super class using: super.variableName
Let’s see this with the help of example.

  1. Person class has name variable.
  2. Employee class also has same name variable.

You can use super.name to refer to Person’s class name variable, there is no other way to access name variable in subclass.

package com.shishirkant;
 
public class Person {
String name;
 
public Person() {
System.out.println("Calling Person constructor");
name = "Default";
}
}
 
class Employee extends Person {
 
String name;
int age;
 
public Employee() {
System.out.println("Calling Employee class constructor");
this.name = "Martin";
}
 
public void workOnAssignment() {
// Working on assignment
}
 
public String getName() {
return name;
}
 
public void setName(String name) {
this.name = name;
}
 
public int getAge() {
return age;
}
 
public void setAge(int age) {
this.age = age;
}
 
public void printName() {
System.out.println("Printing default name from person class : " + super.name);
}
 
public static void main(String args[]) {
Employee e1 = new Employee();
 
e1.printName();
}
}

When you run above program, you will get below output:

Calling Person constructor
Calling Employee class constructor
Printing default name from person class : Default


As you can see, we have used super.name to print name variable from the Person class in Employee class’s printName() method.

Usage of super at Constructor level

super keyword can be used to call the constructor of immediate parent class.

 Important point

Please note that super should be first statement in child class constructor.

Let’s see with the help of example:

package com.shishirkant;
 
public class Person
{
String name;
public Person(String name)
{
this.name=name;
System.out.println("Calling Person Parameterized constructor");
}
}
class Employee extends Person{
 
int age;

public Employee(String name)
{
super(name);
System.out.println("Calling Employee class constructor");
}
public void workOnAssignment()
{
// Working on assignment
}

public String getName() {
return name;
}
 
public void setName(String name) {
this.name = name;
}
 
public int getAge() {
return age;
}
 
public void setAge(int age) {
this.age = age;
}
 
public static void main(String args[])
{
Employee e1=new Employee("John");

System.out.println("Employee's name:"+e1.getName());
}
}

When you run above program, you will get below output:

Calling Person Parameterized constructor
Calling Employee class constructor
Employee’s name:John

SuperConstructorCall


As you can see we have used super keyword to call Person constructor from employee class.

Did you know?

When you explicitly call parameterized constructor with super(paramerters), then complier won’t call no argument constructor of Parent class.

Usage of super keyword at method level

super keyword can be used to call method of Parent class. It can be used to specifically call method of parent class in case of method overriding.

Let’s see with the help of example:

We have printName() method in Person and Employee class and we will super.printName() to call Person’s printName() method.

package com.shishirkant;
 
public class Person
{
String name;
public Person()
{
System.out.println("Calling Person constructor");
name="Default";
}
public void printName()
{
System.out.println("Printing default name from person class : "+this.name);
}
}
class Employee extends Person{
 
String name;
int age;
 
public Employee()
{
System.out.println("Calling Employee class constructor");
this.name="Martin";
}
public void workOnAssignment()
{
// Working on assignment
}
 
public String getName() {
return name;
}
 
public void setName(String name) {
this.name = name;
}
 
public int getAge() {
return age;
}
 
public void setAge(int age) {
this.age = age;
}
public void printName()
{
super.printName();
System.out.println("Printing name from Employee class : "+this.name);
}
 
public static void main(String args[])
{
Employee e1=new Employee();
e1.printName();
}
}

When you run above program, you will get below output:

Calling Person constructor
Calling Employee class constructor
Printing default name from person class : Default
Printing name from Employee class : Martin

SuperMethodCall


As you can see, we have called Person class method from Employee class using super keyword.

What if subclass doesn’t have same method name as superclass?

You don’t need to use super keyword in case subclass doesn’t have the same method name as superclass.

Let’s see with the help of example.

package com.shishirkant;
 
public class Person
{
String name;
public Person()
{
System.out.println("Calling Person constructor");
name="Default";
}
public void printNameSuperClass()
{
System.out.println("Printing default name from person class : "+this.name);
}
}
class Employee extends Person{
 
String name;
int age;
 
public Employee()
{
System.out.println("Calling Employee class constructor");
this.name="Martin";
}
public void workOnAssignment()
{
// Working on assignment
}
 
public String getName() {
return name;
}
 
public void setName(String name) {
this.name = name;
}
 
public int getAge() {
return age;
}
 
public void setAge(int age) {
this.age = age;
}
public void printName()
{
printNameSuperClass();
System.out.println("Printing name from Employee class : "+this.name);
}
 
public static void main(String args[])
{
Employee e1=new Employee();
e1.printName();
}
}
 

Output:

Calling Person constructor
Calling Employee class constructor
Printing default name from person class : Default
Printing name from Employee class : Martin

As you can see, we did not use super with printNameSuperClass(), and it executed successfully.

Follow Us On