Javatpoint Logo
Javatpoint Logo

Difference Between this and super in Java

The 'this' and the 'super' keywords are reserved words that are used in a different context. Besides this, Java also provides this() and super() constructors that are used in the constructor context. In this section, we will discuss the differences between this and super keyword and this() and super() constructor, in Java.

super keyword and super() constructor

super Keyword

A reserved keyword used to call the base class method or variable is known as a super keyword. We cannot use the super keyword as an identifier. The super keyword is not only used to refer to the base class instance but also static members too.

super() Constructor

The super() is mainly used for invoking base class member functions and constructors.

Let's take an example of both the super keyword and super() to understand how they work.

SuperExample1.java

Output:

this vs super in Java

In the main() method, we have made a statement new SuperExample1(). It calls the constructor of the SuperExample1 class.

Inside the constructor, we have made statement super() which calls the constructor of its parent class, i.e., Cat. In the constructor, we have made three statements:

  1. Initialize color with value 'Brown'.
  2. Print parent class data member.
  3. Print current class data member.

When the second statement executes, the flow of the program jumps to Animal class to access the value of its data members. After accessing it, the flow comes back to the Cat class constructor and prints it. After that, the last statement executes and prints the value of the variables of the current class.

After execution of the last statement of the Cat class, the flow comes back to the constructor of class SuperExample1 and executes the remaining statements.

After completing the execution of the SuperExample1(), the flow comes back to the main() method and executes the remaining statements.

Note: In order to use the super(), we have to make sure that it should be the first statement in the constructor of a class. We can use it to refer only to the parent class constructor.

this keyword and this() constructor

this keyword

It is a reserved keyword in Java that is used to refer to the current class object. It is a reference variable through which the method is called. Other uses of this keyword are:

  • We can use it to refer current class instance variable.
  • We can use it to invoke the current class method (implicitly).
  • We can pass it as an argument in the method and constructor calls.
  • We can also use it for returning the current class instance from the method.

this() Constructor

The constructor is used to call one constructor from the other of the same class. Let's take an example of both this keyword and this() to understand how they work.

ThisExample1.java

Output:

this vs super in Java

Difference Between this and super keyword

The following table describes the key difference between this and super:

this super
The current instance of the class is represented by this keyword. The current instance of the parent class is represented by the super keyword.
In order to call the default constructor of the current class, we can use this keyword. In order to call the default constructor of the parent class, we can use the super keyword.
It can be referred to from a static context. It means it can be invoked from the static context. It can't be referred to from a static context. It means it cannot be invoked from a static context.
We can use it to access only the current class data members and member functions. We can use it to access the data members and member functions of the parent class.

Difference Between this() and super() Constructor

this() super()
The this() constructor refers to the current class object. The super() constructor refers immediate parent class object.
It is used for invoking the current class method. It is used for invoking parent class methods.
It can be used anywhere in the parameterized constructor. It is always the first line in the child class constructor.
It is used for invoking a super-class version of an overridden method. It is used for invoking a super-class version of an overridden method.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA