×

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 KeywordSuper 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() constructorsuper() 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.

Related Topics

Anonymous Function in Java

A function defined as being unbound from an identifier is called an anonymous function. Because they permit access to variables within the scope of the contained function, these are a...

4 minutes read.

Java program to find frequency of characters in a string

In this article, you will understand the how to find the frequency of characters in strings by using Java programming language. Along with this, you will understand the hashing concept...

3 minutes read.

Java Swing

Java Swing is the extension of Abstract Windows Toolkit (AWT). Any component designed in Swing will appear the same on any platform. Problems/Disadvantage of Abstract Windows Toolkit (AWT): As we know that...

27 minutes read.

Convert JSON File to String in Java

Before understanding the conversion of JSON file to string, one must know about JSON. What is JSON? JSON stands for JavaScript Object Notation. It is an open standard format lightweight, text-based, and...

3 minutes read.

Java Thread Lifecycle: States and Stages

Multithreading Multithreading is one of the most important features in object-oriented programming, and this multithreading can be implemented by various object-oriented programming languages like Java, Python, and C++. A multithreaded process...

7 minutes read.

Difference Between Thread.start() and Thread.run()

In the Java programming language, the multi-threading concept consists of the start() and run() methods. Thread.start(): The thread's execution is initiated by invoking the start() method. The start() method operates two threads...

4 minutes read.

Java 9 Try With Resources

Java 9 provides the improvement in the try statement. It allows us to declare a try statement with duly declared resources. Whenever the user does not require the functionality with...

3 minutes read.

Java Full Stack

A person who can create both the front end and back end of an application is a full-stack developer. In essence, the term "Java full-stack" refers to a web developer...

9 minutes read.

How to find the length of an Array in Java

Arrays: An array is a sort of container object that stores constant quantities of values of a single type in one memory area. A finite number of items must all be...

3 minutes read.

Java Math nextDown() Method

The nextDown() method of Math class returns the floating-point number adjacent to the argument in direction of the negative infinity. Syntax: public static double nextDown (double d)public static float nextDown (float f) Parameters: The...

2 minutes read.

Duodecimal in Java

Duodecimal is a notation style in which a number with a base of 12 is referred to be a duodecimal number. In Java, we can use to convert duodecimal integers...

2 minutes read.

Java Interface Lock

A synchronisation method called the Lock interface is available as of JDK 1.5. It is comparable to a synchronised block but more complex and versatile. The package java.util.concurrent contains the...

4 minutes read.

Java URL Class with Example

Java URL Uniform Resource Locator To find any resource on the internet, you need to have an address of it. The URL and IP addresses are the pointers used for this purpose....

12 minutes read.

Constructor Program in Java

Constructor Program in Java In Java, a constructor is a piece of code that is used to create an object. A constructor is called implicitly when an object is created in...

7 minutes read.

Nonagonal number in Java

Figureate numbers with the form n(7n-5)/2 are known as nonagonal numbers. 7n+3 will be a triangular number if n is a nonagonal number. The nonagon is included in the idea...

4 minutes read.

How to remove last character from String in Java

In java, there are predominantly three classes connected with the string. The classes are String, StringBuilder, and StringBuffer class that gives techniques connected with string control. Eliminating the first and...

5 minutes read.

Callable Statement in Java

The Callable statement in Java is used to call the functions and Stored procedures. Example: If we want to know about the age of a person based on their date of birth,...

3 minutes read.

Three-way operator in Java

The ternary operator in Java is the only conditional operator that takes three operands. It's a popular one-line substitute for the if-then-else expression among Java programmers. If-else clauses can be...

3 minutes read.

What is String in Java?

What is String in Java? Strings are a collection of characters that are commonly used in Java programming. Strings are regarded as objects in the Java programming language. “String” is a Java...

4 minutes read.

Java 16

Java 16 is the most recent short-term incremental release, based on Java 15, and it was released on March 16, 2021. Records and sealed classes are just two of the...

11 minutes read.