Super and this in Java

Super and this are very important concepts that needs to be understood by every java developer. We will be understanding and highlighting some of the very key points that has to be kept on your fingertips.

Super is used to access immediate base/parent class instance variable or method while this is used to access class instance variable or method of the current class. Also it means that if we make a call to super(), it refers to the default constructor of the immediate base class. Similarly if we make a call to this(), it refers to the default constructor of the current class.

If we don’t create any explicit constructor for a class, a default constructor is provided by the compiler automatically. This default constructor provided by the compiler always has an implicit call to super() as its first statement. The same applies for custom explicitly defined constructors as well, where the first statement in the constructor is an implicit call to super() by default, unless we override that call by an explicit call to a parametrized or non-parametrized super() or this() method. The below example provided explains it all. Do make sure we all understand how the below snippet works.

Note that, an implicit call to super() in a child/derived class constructor can only be overridden by an explicit call to a parametrized or non-parametrized super() or this() method, as is applicable.

Refer to the below code snippet to understand the overall concept of super and this

I hope the above snippet I have chosen proves very beneficial and efficient in understanding the concept. Make sure you have gone through all the comments that has been mentioned in the above snippet.


When we run the above class as Java application, we get the following output –

Receive our updates to your inbox

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.