Difference between Super and This Keyword in Java
In this tutorial, we will understand the basic difference between super and this keyword in Java. So, let’s start with the basic introduction of both.
Introduction to ‘super’ Keyword:
The ‘super’ keyword in Java is a reference variable that refers to an immediate parent class object. It comes into the picture with the concept of inheritance in Java.
When we create an object of subclass, an instance of superclass is created implicitly, which is referred by super reference variable. The keyword ‘super’ allows us to access members of the superclass.
When a class extends another class, it inherits the attributes and behaviors of the superclass. By using ‘super’, we can access and utilize those inherited members in the subclass.
Another application of the ‘super’ keyword is invoking methods from the superclass. If a subclass overrides a method from the superclass and wants to use the superclass’s implementation, we can use ‘super.methodName()’ to explicitly call the superclass method.
Introduction to ‘this’ Keyword:
The keyword ‘this’ in Java is a reference variable that refers to the current instance of the class. It is commonly used to differentiate between instance variables and method parameters with the same name.
One of the practical applications of the ‘this’ keyword is constructor chaining. When a class has multiple constructors, each constructor can call another constructor using ‘this()’. This helps reduce redundant code and enhances code reusability.
We can use ‘this’ keyword to:
- refer to the current class instance variable.
- call the non-static method of the current class.
- invoke the current class constructor.
- return the object of the current class from the method.
In addition to these, we can also use as a parameter in the method call and constructor call.
Difference between Super and This Keyword in Tabular Form
The difference between super and this keyword in Java in tabular form is as follows:
SN | ‘this’ Keyword | ‘super’ Keyword |
---|---|---|
1. | “this” is a reference variable that contains current class objects. | “super” is a reference variable that contains immediate super class objects. |
2. | Any member of the current class object from within an instance method or a constructor can be referred by using this keyword. | If the method overrides one of its superclass’s method, the overridden method can be called through the use of super keyword. |
3. | ‘this’ keyword is used to call another constructor from within a constructor in the same class. | ‘super’ keyword is used to call the superclass’s constructor from within a constructor of the subclass. |
4. | Java compiler never puts automatically this() keyword like super(). | By default, the compiler automatically puts the super() keyword at first line inside the constructor. |
In this tutorial, we have explained the difference between super and this keyword in the table. Hope that you will have understood all points discussed in the tabular form. Stay tuned with the next tutorial.
Thanks for reading!!!