super and this keywords in Java
Last Updated :
08 Jun, 2022
In java, super keyword is used to access methods of the parent class while this is used to access methods of the current class.
this keyword is a reserved keyword in java i.e, we can’t use it as an identifier. It is used to refer current class’s instance as well as static members. It can be used in various contexts as given below:
- to refer instance variable of current class
- to invoke or initiate current class constructor
- can be passed as an argument in the method call
- can be passed as argument in the constructor call
- can be used to return the current class instance
Example
Java
class Parent {
int a = 36 ;
static float x = 12 .2f;
}
class Base extends Parent {
void GFG()
{
super .a = 1 ;
System.out.println(a);
super .x = 60 .3f;
System.out.println(x);
}
public static void main(String[] args)
{
new Base().GFG();
}
}
|
- super is a reserved keyword in java i.e, we can’t use it as an identifier.
- super is used to refer super-class’s instance as well as static members.
- super is also used to invoke super-class’s method or constructor.
- super keyword in java programming language refers to the superclass of the class where the super keyword is currently being used.
- The most common use of super keyword is that it eliminates the confusion between the superclasses and subclasses that have methods with same name.
super can be used in various contexts as given below:
- it can be used to refer immediate parent class instance variable
- it can be used to refer immediate parent class method
- it can be used to refer immediate parent class constructor.
Example
Java
class RR {
int first = 22 ;
int second = 33 ;
void garcia( int a, int b)
{
a = this .first;
b = this .second;
System.out.println(first);
System.out.println(second);
System.out.println(a);
System.out.println(b);
}
void louis( int m, int n)
{
this .first = m;
this .second = n;
System.out.println(first);
System.out.println(second);
System.out.println(m);
System.out.println(n);
}
public static void main(String[] args)
{
new RR().garcia( 100 , 200 );
new RR().louis( 1 , 2 );
}
}
|
Similarities in this and super
- We can use this as well as super anywhere except static area. Example of this is already shown above where we use this as well as super inside public static void main(String[]args) hence we get Compile Time Error since cannot use them inside static area.
- We can use this as well as super any number of times in a program.
- Both are non-static keyword.
Example
Java
class Vehicle {
Vehicle() { System.out.println( "Vehicle is created." ); }
}
class Bike extends Vehicle {
Bike() { System.out.println( "Bike is created." ); }
Bike(String brand)
{
super ();
this ();
System.out.println( "Bike brand is " + brand);
}
}
public class GFG {
public static void main(String args[])
{
Bike bike = new Bike( "Honda" );
}
}
|
100
600
9000
See above we have used this 3 times. So this can be used any number of times.
1
60.3
See above we have used super 2 times. So super can be used any number of times.
Note: We can use ‘this’ as well as ‘super’ any number of times but main thing is that we cannot use them inside static context.
Let us consider a tricky example of this keyword:
Java
class RR {
int first = 22 ;
int second = 33 ;
void garcia( int a, int b)
{
a = this .first;
b = this .second;
System.out.println(first);
System.out.println(second);
System.out.println(a);
System.out.println(b);
}
void louis( int m, int n)
{
this .first = m;
this .second = n;
System.out.println(first);
System.out.println(second);
System.out.println(m);
System.out.println(n);
}
public static void main(String[] args)
{
new RR().garcia( 100 , 200 );
new RR().louis( 1 , 2 );
}
}
|
Output:
//it is of S.O.P(first) of garcia method
22
//it is of S.O.P(second) of garcia method
33
//it is of S.O.P(a) of garcia method
22
//it is of S.O.P(b) of garcia method
33
//it is of S.O.P(first) of louis method
1
//it is of S.O.P(second) of louis method
2
//it is of S.O.P(m) of louis method
1
//it is of S.O.P(n) of louis method
2
Flow of program : First, it start from main and then we have new RR().garcia(100, 200) then flow goes to garcia method of RR class and then in that we have:
a = this.first
b = this.second
Here, what is happening is that the value of instance variable(i.e, first) and the value of static variable(i.e, second) are assigned to the garcia method’s local variables a and b respectively. Hence value of a and b comes out to be 22 and 33 respectively. Next we have new RR().louis(1, 2) hence here flow goes to the louis method of RR class and in that we have:
this.first = m
this.second = n
Here, above what happens is that the value of the louis method’s local variables i.e, m and n are assigned to the instance as well as static variables i.e, first and second respectively.
Hence, the value of first and second variables are same which we have passed into the louis method i.e, 1 and 2 respectively.
Can we use both this() and super() in the same constructor?
An interesting thing to note here is that according to Java guidelines, this() or super() must be the first statement in the constructor block for it to be executed. So we cannot have them together in the same constructor as both need to be the first statement in the block for proper execution, which is not possible. Trying to do so would raise a compiler error.
Java
class Vehicle {
Vehicle() { System.out.println( "Vehicle is created." ); }
}
class Bike extends Vehicle {
Bike() { System.out.println( "Bike is created." ); }
Bike(String brand)
{
super ();
this ();
System.out.println( "Bike brand is " + brand);
}
}
public class GFG {
public static void main(String args[])
{
Bike bike = new Bike( "Honda" );
}
}
|
On running the above program, we get an error on line number 12 saying that “call to this must be first statement in constructor”. This is because we tried using both super() and this() together. While super() was the first line of the constructor, this() statement violated the condition that it should be the first line, hence raising an error.
Compile Errors:
prog.java:12: error: call to this must be first statement in constructor
this();
^
1 error
Share your thoughts in the comments
Please Login to comment...