Java provides two useful implicit reference variables ‘this’ and ‘super’. Both are used to resemble the reference variable pointing to the superclass or subclass.
Both keywords are frequently used and if you want to read them thoroughly links are provided here. ( for ‘this’ keyword and for ‘super’ keyword).
The basic difference between this and super is that this reference variable is of subclass type, whereas super reference variable is of superclass type
Difference between this and super keywords in Java
'this' keyword | 'super' keyword |
---|---|
this keyword is an implicit reference variable ( that holds physical address of an object) that is used to refer current class objects | super keyword is an implicit reference variable ( that holds physical address of an object) that is used to refer child class's immediate parent class objects |
It invokes the constructor of subclass | It invokes the constructor of superclass |
In the case of the overridden methods( a method which is present in both parent and child with the same signature but with meaningful implementation), 'this' keyword is used to invoke the method of subclass class | In the case of the overridden method( a method which is present in both parent and subclass with the same signature but with meaningful implementation), 'super' keyword is used to invoke the method of superclass |
It solves the problem of variable hiding. Using 'this' keyword, a compiler always refers to instance variable of the current class | Using super keyword, compiler always refer to immediate parent class instance variable ( in case of a variable having the same name in parent and subclass) |
It can be used to return and pass as an argument in context of current class object | It can be used to return and pass as an argument in context of parent class object |
This keyword
In Java, this keyword is used to refer to the current class object.
There are various situations where sometimes is important to mention the object that actually invoked a particular method, constructor and initialization of instance variable. It can be used to invoke current class constructor, method, instance variable and many more.
Example of ‘this’ keyword
class sample1{ int m; int n; sample1() { m=5; n=10; } void display() { System.out.println(" Value of M = "+m+" Value of N = "+n); } } class sample extends sample1{ int x; int y; sample(int a,int b) { x=a; y=b; } void callchecker() { this.display(); } @Override void display() { System.out.println(" Value of x = "+x+" Value of Y = "+y); } } public class ThisKeyword { public static void main(String[] args) { sample obj = new sample(10,20); obj.callchecker(); } }
Super keyword
In Java, super keyword is used to refer to the parent class object. It deals with various situation, but the most importantly, it helps in resolving the name conflict between member variables of superclass and subclass having same name.
Example of ‘super’ keyword
class A { int z; A(int a) { z=a; } public void f1() { } } class B extends A { int z; B(int a, int b) { super(a); //call for super class constructor z=b; } public void f1() { super.f1(); //call for super class method } public void f2() { super.z=4; //call for super class variable } } public class Example { public static void main(String[] args) { B obj = new B(); B obj = new B(5,3); obj.f1(); obj.f2(); } }