Difference between this and super in Java
In this article, you will be very well equipped with the knowledge of this keyword and super keyword in java. You will be able to understand where to implement this and super keyword and also understand the differences between them.
This Keyword
In Java, this keyword has many functionalities. But three main functionalities are:
1. To identify existing class instance variable
The existing class instance variable can be referred to using this keyword. This keyword removes the ambiguity issue if it exists between both the instance variables and the parameters.
Let us understand this with an example
File name: Dentists.java
class Doctor{
String name;
float sal;
Doctor(String name,float sal){
this.name=name;
this.sal=sal;
}
void print(){
System.out.println(name+" "+sal+" ");
}
}
class Dentists{
public static void main(String args[]){
Doctor d1=new Doctor("Rama Rao",50000f);
Doctor d2=new Doctor("Nageshwar Rao",60000f);
d1.print();
d2.print();
}}
Output
Rama Rao 50000.0
Nageshwar Rao 60000.0
2. To execute the existing class method
With this keyword, you can access a method of the existing class. If somehow the this keyword is not used, the compiler will dynamically add it when implementing the method.
Let us understand the above statement with an example.
File name: Thisant.java
class Ant
{
void move()
{
System.out.println("Moving");
}
void eat()
{
System.out.println("Eating");
this.move();
}
}
class Thisant{
public static void main(String args[]){
Ant c=new Ant();
c.eat();
}
}
Output
Eating
Moving
3. This keyword can also be leveraged to access the existing class this() constructor, should use this() constructor call. To put it in different perspective, it is used for chaining the constructor.
Super Keyword
In Java, the super keyword is just a reference variable that is used to access to the object of the direct parent class.
When you establish a subclass instance, an implicit instance of the parent class is also generated and is linked to the super reference variable.
Super keyword has many functionalities. But the main functionalities are
1.When referring to an actual parent class instance variable, super keyword comes into play.
Let us understand this with an example
File name: Superbird.java
class Birds
{
String col="Lavneder";
}
class Sparrow extends Brids{
String col="Yellowish-Brown";
void showCol(){
System.out.println(col);
System.out.println(super.col);
}
}
class Superbird{
public static void main(String args[]){
Sparrow z=new Sparrow();
z.showCol();
}}
Output
Lavender
Yellowish-Brown
2. Users can access the parent class method with super keyword.
The super keyword may also be employed to execute parent class method. If the subclass and parent class both contain the same method, it needs to be used. It is implemented,if the method is overridden.
class Fish{
void swim()
{System.out.println("swimming");}
}
class Dolphin extends Fish{
void swim()
{System.out.println("swimming faster");}
void sleep()
{System.out.println("sleeping");}
void do(){
super.swim();
sleep();
}
}
class Superfish{
public static void main(String args[]){
Dolphin k=new Dolphin();
k.do();
}}
Output
swimming
sleeping
Differences
The reserve words "this" and "super" are employed in a distinct context. In addition to this, Java offers constructors named this() and super() that are employed in the constructor context.
Differences between this keyword and super keyword
this Keyword | Super Keyword |
this keyword denotes the existing instance of the class. | The super keyword specifies the existing instance of the base classes. |
this keyword can be used to call the current class's default constructor. | In order to access the default function Object() { [native code] } of the base classes, we can employ the super keyword. |
It is possible to refer in a static context. It implies that it can be used in the static environment. | It is unusable when used in a static setting. this indicates that it can't be called from a static environment. |
Only the existing class components and associated operations are accessible through it. | It enables us to use the parent class's member methods and data elements.. |
Distinctions between this() and super() constructors
this() constructor | super() constructor |
The existing class object is referred through this() constructor. | The super() constructor corresponds to an object of the current class. |
It is adapted to call the active class method. | It is served to call methods from parent classes. |
In the parameterized constructor, it can be implemented anywhere. | It is invariably the first line of the child class constructor. |
It is used to call a super-class equivalent of a method that has been overridden. | It is employed to call an overridden method's super-class equivalent. |