Java this vs super Keywords Comparison.
By: Navoda Nilakshi
Tags: Java, Oop Concepts, Coding, Object Oriented, Oop

Hey everyone! Sometimes you may be a regular user of “this” and “super” keywords. Maybe, you’re wondering what they are used for. This article will give you a deeper insight about these keywords, their application and also a bit of comparison between them. Without further ado, Let’s dive in!

To begin with, there are two keywords in java called super and this. Also there are two method calls named super() and this(). I will elaborate first on the keywords as it will broaden your understanding about this topic.

super keyword in java is used to access member variables and methods of the parent class.

this keyword on the other hand is used to access member variables and methods in the current class.

super keyword

super keyword is commonly used when we override parent class methods as it’s demonstrated in the example below.

If we don’t use the super keyword when calling printMessage() , it can result in a recursive call. Which means the same method is calling itself.

super keyword can be used anywhere in a java class except in static areas.

this keyword

this keyword on the other hand is used to access member variables and methods in the current class. this keyword is especially helpful when we have a parameter with the same name as an instance variable.

this keyword is prominently used in constructors and setter methods. Let me explain to you as to why we use this keyword in those concerns. Constructors and setters may have parameters and instance variables used inside them with the same names. So, this can induce logical errors in the output that you’re getting. So to avoid this conflict, it’s advisable to use this keyword to refer to the current class object.

In getter methods you can optionally use it as you wish. This is because in getters we don’t pass parameters. So there’s no chance for the above conflict to happen.

Consider the following example:

As you can see, in the constructor and in setter, parameters with the same name as instance variables have been used. So it is important to use this keyword in those contexts.

this keyword can be used anywhere in a java class except in static areas.

this() vs super()

Now it’s time to have a quick glance at the usage of this() method and super() method calls. Both of them can only be used inside a constructor. Both of them are constructor calling methods from another constructor. The difference is this() calls another overloaded constructor in the same class while super() calls parent class constructor.

You can use either this() method or super() method depending on your need but the bottom line here is that you can’t use both of them together in the same constructor.

Refer the following examples:

This is an example for the use of this() method:

And below is an example for the use of super() method:

You might wonder why we can’t use both of them together. The reason is whenever you use this() or super(), that method call has to be the first line inside the constructor block. Due to this reason it’s impossible to use them together without dealing with errors.

I hope this article gave you a quick overview about this and super keywords as well as this() and super() method calls in Java. Cheers!