Super This Keyword in Java

Super in java is a keyword which is a reference variable. It is used to refer to the immediate superclass object or instance variable. The word super came into usage because of the concept of inheritance. The keyword super is placed inside a subclass to call a method from a superclass.

Let's say we have a super class as Person and Employee as a sub class as shown below:

Now, if we see Employee class's printStudent() method, there are two ways to call age variable of Person class.

First way is by creating object of Person class. But this is not an affordable option as just for sake of one variable age here we are loading complete class Person, which wastes memory unnecessarily.

Second way is to just call age by using super keyword which will not waste memory and we can achieve our objectives as well

Other two options are to print local variable age or print global variable of same class.

Note : We use the super keyword to bypass class variables and method’s local variable with same name while calling and we need not to create objects. It's all about memory management.

Remember the points given below:

  • We must have super class subclass relationship in our program.
  • Super can be applied to variable, constructors, and method.
  • Super keyword always represents super class object.
  • It's generally used to bypass global variable with the same name.

super.a; //for Variable
super.m1(); //for Method
super(); // for Constructor

  • To call super class variable or method we have two options
super keyword in java with example

A a=new A(); //1st way

super.m2(); //2nd way

  • 1st way : In this case we bring all members of capital A in to RAM, out of that we are just calling m2(), so unnecessary memory will get allocated for another members.

  • 2nd way : The other option super.m2()

    • There is no super.super in java

    • In any of our class's constructors if we don't write any call to constructor of parent class by using super like super(), then JVM puts super call to constructor in the first line of every constructor wherever you did not add your own call to constructor.

Example 1

Example 2

  • Super call to constructor must be at first line of all constructors.

  • We strictly cannot call super () in methods in any line; this rule applies only to super() not to super.a or super.m1().

  • Recursion by using super call to constructor will result in compile time error in java.

The compile time error is shown below :

Note: In this example, recursion between super call to constructor will be there because we are extending same class itself. Consider invisible super() inside constructor.

Observe the example below to see how super keyword navigates the flow of our program.

This keyword in java is used inside a method or constructor of a class. It is used to refer to a member of a current object within the instance of a method or constructor.

  • This represents current class object
  • Can be applied to variable, constructor and method

this.a; // variable case
this.m1(); // method case
this(10); // To call parameterize constructor of current class object

  • In case of variable, same class’s global variable will get called

  • Call to constructor by using this ‘this()’ must be a first line of constructor only. This means that we cannot add ‘this()’ anywhere other than the first line of constructor

  • inside method ‘this()’ is not allowed, that is call to constructor not allowed by using this.

  • JVM never puts automatically this() keyword like super()

  • If we wrote call to constructor explicitly by using ‘this()’ the ‘super()’ call to constructor will not be put by JVM

  • Recursion will be there as we call the same class constructor using this() in same constructor, which is not allowed in java

Example 1

Note : To understand this example in more detail

  • Go on commenting every variable start from m1 of Class D then run the program
  • Comment global variable of class D then run
  • Comment C class's global variable then again run and observe output

Example 2

Super This
  • super is reference variable which contains immediate super class objects
  • super also can be used in two ways:
  • a) To invoke immediate super class variable and methods.
    • super.n;
    • super.m1();
  • b) to invoke immediate super class constructor
  • Ex :
  • ‘this’ is a reference variable which contains current class objects
  • 'this' can be used in two ways:
  • a) To invoke current class variable and methods
    • this.a;
    • this.m1();
  • b)To invoke current class constructors
  • Ex:
    this (10,20);
  • super reference variable is instance reference variable and cannot be accessed from static context
  • This reference variable is instance reference variable and cannot be accessed from static context
  • Call to super() must be at the first line of constructor only
  • Call to this() must be at the first line of constructor only