This post will discuss the difference between this and super keyword in Java. We will also cover the difference between this() and super() method in Java.

1. Overview of this keyword

The this keyword is a reference to the current object as an instance of the current class. It can be used to access the fields and methods of the current class, as well as to invoke the constructors of the current class. The this keyword can be used in four contexts:

1. To access fields of the current class

The this keyword can be used to access the instance variables of the current class, especially when they are shadowed by local variables or parameters with the same name. For example:

2. To invoke constructors of the current class

The this keyword can be used to invoke another constructor of the same class within a constructor. This is useful when there are multiple constructors with different parameters, and some common code needs to be executed by all of them. For example:

3. To invoke methods of the current class

The this keyword can be used to invoke the instance methods of the current class. For example:

Download  Run Code

4. To return current object from a method

The this keyword can be used to return the reference of the current object from a method. This is useful when we want to chain multiple methods on the same object, or when we want to pass the current object as a parameter to another method. For example:

Download  Run Code

2. Overview of super keyword

The super keyword is a reference to the current object as an instance of the parent class. It can be used to access the fields and methods of the parent class, as well as to invoke the constructors of the parent class. The super keyword can be used in below contexts:

1. To invoke constructor or access fields of the parent class

The super keyword can be used to invoke the constructor of the parent class from the constructor of the current class. This is useful when we want to initialize some common data or logic in the parent class before initializing the current class. It can also be used to access the instance variables of the parent class, especially when they are hidden by instance variables of the subclass with the same name. For example:

Download  Run Code

2. To invoke methods of the parent class

The super keyword can be used to invoke the method of the parent class from the method of the current class. This is useful when we want to reuse some common code or logic in the parent class or override some behavior in the current class. For example:

Download  Run Code

3. Difference between this and super keyword

The functionality of this and super keyword are similar. They both are reference variables that can be used to access the fields, methods, and constructors of a class. However, they also have some differences that need to be understood clearly:

  • The this keyword points to the current object of the class, while the super keyword points to the parent object of the current object of the class.
  • The this keyword can be used to access the fields, methods, constructors, and current object of the current class, while the super keyword can be used to access the fields, methods, and constructors of the parent class from the subclass.
  • The this keyword can be used in any method or constructor of a class, while the super keyword can be used only in a subclass method or constructor.
  • The this keyword can be used as an argument to another method or constructor, while the super keyword cannot be used as an argument.
  • The this keyword can be used as a return value from a method, while the super keyword cannot be used as a return value.

That’s all about the differences between this and super keyword in Java.