×

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

How to Reverse a String in Java

How to Reverse a String in Java There are a lot of ways to reverse a string in Java. One can use iteration, StringBuilder, StringBuffer to do the reverse of a...

6 minutes read.

Java Integer rotateLeft() method

The rotateLeft() method of Java Integer class returns the value obtained by rotating the  2’s complement binary representation of the given integer value left by the specified number of bits. Syntax public...

1 minute read.

How to convert String to String array in Java

A String in Java is a thing that indicates a collection of letters. We must include the String class from java.lang package if we want to be using strings. An...

5 minutes read.

Abstract classes in Java

Abstract classes in Java Java uses the 'abstract’ keyword to make a class abstract. Such classes are known as abstract classes, and the process is known as abstraction. In programming language, abstract...

4 minutes read.

Aggregation in Java

Aggregation A "has-a" and "whole/part" connection is the best way to define the aggregate relationship in Java, which exists between two classes. This connection relationship has a higher level of specialisation....

4 minutes read.

Java Integer compareUnsigned() method

The compareUnsigned() method of Integer class compares two int objects numerically by treating the values as unsigned. Syntax public static int compareUnsigned(int x , int y) Parameters The parameters ‘x’ and ‘y’ represent the...

1 minute read.

Java Boolean logicalXor() Method

The logicalXor() method of Java Boolean class returns the result of implementing logical XOR operation on the specified Boolean operands. Syntax: public static boolean logicalXor (boolean a, boolean b) Parameters: The parameters ‘a’ and...

2 minutes read.

Swapping Program in Java

Swapping Program in Java The swapping program in Java is used to interchange the values of the two variables. For example, if X = 12 and Y = 24, then the...

4 minutes read.

Java Logo

Java is a prominent and extensively used object-oriented programming language. In 1995, Sun Microsystems created it. Later in 2009, Oracle Corp takeover Java. History of Java Logo The name of the island...

3 minutes read.

How to Install Java on MAC

There are many possible ways to install java on mac. This article is based on the installation of java on mac. The operating system platform is Mac OS X, macOS and...

3 minutes read.

String Handling Method in Java

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

4 minutes read.

Java String Inbuilt functions

There are different types of inbuilt functions available in the Java String class, all of them are listed below. Char chat At (int index)It outputs the character indicated by the index....

5 minutes read.

Java Byte intValue() method

The intValue()  method of Java Integer class returns an int value for this Integer.  Syntax public int intValue()  Parameters NA  Specified by This method is specified by intValue in class Number  Return Value This method returns the numeric...

1 minute read.

How to create a mirror image of a 2D array in Java

Problem Statement We have provided a list of m x n. here m indicates rows, and n indicates columns). Printing the fresh matrices should result in a mirror reflection of an...

2 minutes read.

Functional Interface in Java 8

A brief introduction to Interface in Java: Interfaces in Java are basically the blue print of classes. Before the appearance of Java 8, it was only possible to declare one or...

11 minutes read.

Mutable class in Java

A language for object-oriented programming is Java. Because this is an object-oriented language of programming, all of its mechanisms and methods are based on objects. Java has a concept of...

6 minutes read.

How to Convert char to String in Java

How to Convert char to String in Java There are two methods to convert char to String: Using String.valueOf(char) method Using Charcter.toString(char) method Using String.valueOf(char) method valueOf(char) is the static method of String class that...

2 minutes read.

Association in Java

In Java, association refers to the link between two classes established by their objects. One-to-one, one-to-many, and many-to-many connections are managed via association. The Association defines the multiplicity between objects...

5 minutes read.

How to Create Different Packages for Different Classes in Java

Packages in Java In Java, Packages are an assortment of classes, sub-packages, and connection points. i.e. A package addresses a word reference that contains a connected gathering of styles and points...

7 minutes read.

Accessors and Mutator in Java

Introduction Accessors and mutators are used in Java to get and set the value of private fields, respectively. Accessors and mutators are both referred to as getters and setters, respectively. The...

6 minutes read.