“super” vs “this” in Java

Kasun Dissanayake
6 min readMar 18, 2022

In this tutorial, I am going to explain the difference between “super” and “this” keywords in Java and what scenarios we can use these keywords. These 2 keywords are very important and frequently used in Java.

Super Keyword and Usage

The super keyword is a reference variable that is used to refer to the immediate parent class object. I think you know about parent classes and child classes. The super keyword is used to,

  • refer to the immediate parent class instance variable.
  • to invoke immediate parent class constructor (here we are using the super() method)
  • invoke immediate parent class method

Refer to the immediate parent class instance variable

You may have a question about why we can't use to refer parent class from child class, we can use Inheritance for this. Yes, you are correct!. Inside Child classes, in Inheritance, you can use the super keyword to refer to Parent class attributes and methods. I will show you an example.

class Parent {
String attribute = "Parent Attribute";
}


class Child extends Parent{
String attribute = "Child Attribute";

void method(){
System.out.println(attribute);
}
}
public class SuperExampleClass {
public static void main(String[] args) {
Child child = new Child();
child.method();

}
}

Here you can see the program will print the Child Class attribute. But what will you do when you want to get the Parent attribute. Then super comes into the picture. I am not going to change the whole code. I am just adding a super keyword.

class Parent {
String attribute = "Parent Attribute";
}


class Child extends Parent{
String attribute = "Child Attribute";

void method(){
System.out.println(super.attribute);
}
}
public class SuperExampleClass {
public static void main(String[] args) {
Child child = new Child();
child.method();

}
}

Output:

Not only attributes you can invoke immediate super class constructor by using super keyword.

What is a constructor?

It's similar to a method that does not have any return type and we don't want to invoke this constructor while creating an Object. When you create an object from a class that is enough the constructor also will be exected. Read more about constructors here.

The super() method used to invoke immediate parent class constructor

class Parent {
Parent(){
System.out.println("Parent Class Constructor");
}
String attribute = "Parent Attribute";
}


class Child extends Parent{
Child(){
super();
System.out.println("Child Class Constructor");
}
String attribute = "Child Attribute";

void method(){
System.out.println(attribute);
}
}
public class SuperExampleClass {
public static void main(String[] args) {
Child child = new Child();

}
}

Here I am using the default constructor in Parent Class which does not have any arguments. In the Child class also I am using a default constructor. Inside the child constructor, I am invoking a super class constructor using the super() keyword. It will invoke the immediate parent class constructor. Now execute the program and you can see this will call the child class constructor first and inside the child class constructor, it will call parent class constructor.

If you inherit Parent class to Child class, default constructor will call automatically from the child class. You don’t need to use super() keyword. If you want to invoke parameterized constructor in Parent class you need to use super() keyword.

Now I am defining parameterized constructors in Parent Class as well as in the Child Class. In the Child class, you need to call the parent class parameterized constructor. To call you can pass two values inside the super method. It will first call the child class constructor and inside the child class constructor, it will call the parent class constructor.

class Parent {
Parent(){
System.out.println("Parent Class Default Constructor");
}
Parent(int a, int b){
System.out.println("Parent Class Parameterized Constructor");
}
}


class Child extends Parent{
Child(){
System.out.println("Child Class Default Constructor");
}
Child(int a, int b){
super(a, b);
System.out.println("Child Class Parameterized Constructor");
}

}
public class SuperExampleClass {
public static void main(String[] args) {
Child child = new Child(1,2);

}
}

Output:

Invoke immediate parent class method

You can invoke your parent class method using the super keyword. To invoke that method you can you super keyword like this.

super.methodParent();

This Keyword and Usage

There can be a lot of usage of java as this keyword. In java, this is a reference variable that refers to the current object like “This is my Object”. In java, this keyword is used to define the current Object.

I have created a Student Class that has 3 global variables and then I created a parameterized constructor. Inside the constructor, I have created a local variable and assigned a local variable to a global variable. Furthermore, I have created a method to print Student details. When you come to the parameterized constructor you will be getting confusion seen as to which one is global and which one is local.

public class Student {

int id;
String name;
String address;

public Student(int id, String name, String address) {
id = id;
name = name;
address = address;
}

public void print(){
System.out.println("STUDENT DETAILS: ID: "+ id + " Name: "+ name + " Address: " + address);
}

public static void main(String[] args) {
Student student = new Student(1, "Kasun", "Colombo");
student.print();

}

}

The compiler also will get confused what is the global and local variable. If I run this code I won't get any errors. But I will get null values for the output.

We are getting null values and 0 values for the Integer variable because the compiler does not know which one is local and global. To identify the local and global variables we can use this keyword.

public Student(int id, String name, String address) {
this.id = id;
this.name = name;
this.address = address;
}

Now Java compiler can understand “this.id” refers to the global variable and the “id” refers to the local variable. Now I get the output like this.

So this is why this keyword is used.

I hope you will get a basic idea about “super and this” keywords and their basics. See you again in another tutorial.

Thank You!

--

--

Kasun Dissanayake

Senior Software Engineer at IFS R & D International || Former Software Engineer at Pearson Lanka || Former Associate Software Engineer at hSenid Mobile