Open In App

Difference between super() and this() in java

super and this keyword super() as well as this() keyword both are used to make constructor calls. super() is used to call Base class’s constructor(i.e, Parent’s class) while this() is used to call the current class’s constructor. Let us see both of them in detail:

super() Keyword 

super() is used to call Base class’s(Parent class’s) constructor






// Java program to illustrate usage of
// this() as first statement
  
class RR {
    RR()
    {
        // Uncommenting below line causes compilation
        // error because this() should be first statement
        // System.out.println("Compile Time
        // Error");
        this(51);
        System.out.println(
            " Flow comes back from RR & quot; + "
            class 1 arg const& quot;);
    }
    RR(int k)
    {
        System.out.println("RR class's 1 arg const");
    }
    public static void main(String[] args)
    {
        new RR();
        System.out.println(" Inside main & quot;);
    }
}

Output:

Parent class's No arg constructor
Flow comes back from Parent class no arg const
Inside Main

Flow of Program:



We can use super() only inside constructor and nowhere else, not even in static context not even inside methods and super() should be first statement inside constructor. 




// Java program to illustrate super() by default
// executed by compiler if not provided explicitly

class Parent {
Parent()
{
System.out.println(“Parent class’s No ” +
“argument constructor”);
}
Parent(int a)
{
System.out.println(“Parent class’s 1 argument” +
” constructor”);
}

}

class Base extends Parent {
Base()
{
// By default compiler put super()
// here and not super(int)
System.out.println(“Base class’s No ” +
“argument constructor”);
}
public static void main(String[] args)
{
new Base();
System.out.println(“Inside Main”);
}
}


Output:

Parent class's No arg constructor
Flow comes back from Parent class no arg const
Inside main

Note: super() should be first statement inside any constructor. It can be used only inside constructor and nowhere else. super() is used to refer only parent class’s(super class’s) constructor.

this() Keyword 

this() is used to call the current class’s constructor




// Java program to illustrate recursive
// constructor call not allowed
 
class RR {
    RR() { this(30); }
    RR(int a) { this(); }
    public static void main(String[] args) { new RR(); }
}

Output:

RR class's 1 arg const
Flow comes back from RR class's 1 arg const
Inside Main

Flow of Program:

We can use this() only inside constructor and nowhere else, not even in static context not even inside methods and this() should be first statement inside constructor. 





Output:

RR class's 1 arg constructor
Flow comes back from RR class 1 arg const
Inside main

Note: this() should be first statement inside any constructor. It can be used only inside constructor and nowhere else. this() is use to refer only the current class’s constructor.

Important points about this() and super()

  1. We can use super() as well this() only once inside constructor. If we use super() twice or this() twice or super() followed by this() or this() followed by super(), then immediately we get compile time error i.e, we can use either super() or this() as first statement inside constructor and not both.
  2. It is up to you whether you use super() or this() or not because if we are not using this() or super() then by default compiler will put super() as the first statement inside the constructor. 

Example




// Java program to illustrate super() by default
// executed by compiler if not provided explicitly
  
class Parent {
    Parent()
    {
        System.out.println("Parent class's No " +
                          "argument constructor");
    }
    Parent(int a)
    {
        System.out.println("Parent class's 1 argument" +
                                      " constructor");
    }
  
}
  
class Base extends Parent {
    Base()
    {
        // By default compiler put super()
        // here and not super(int)
        System.out.println("Base class's No " +
                        "argument constructor");
    }
    public static void main(String[] args)
    {
        new Base();
        System.out.println("Inside Main");
    }
}

Output:
Parent class's No argument constructor
Base class's No argument constructor
Inside Main

Flow of program:

Recursive constructor call not allowed 




// Java program to illustrate recursive
// constructor call not allowed
 
class RR {
    RR() { this(30); }
    RR(int a) { this(); }
    public static void main(String[] args) { new RR(); }
}

Output:

Compile time error saying recursive constructor invocation

Flow of program: Here, above starts from main() and then flow goes to No arg constructor of RR class. After that, we have this(30) and flow goes to 1 arg constructor of RR and in that we have this() so again flow goes to No arg constructor of base class and in that again we have this(30) and flow again goes to 1 arg constructor of Base class and it goes on …… like a recursion. So it is invalid that’s why we get a compile-time error saying recursive constructor invocation. So recursive constructor invocations are not allowed in java.

Let us see the differences in a tabular form as follows:

super() keyword this() keyword 
super() calls the parent constructor this() can be used to invoke the current class constructor
It can be used to call methods from the parent. It can be passed as an argument in the method call.
It is returned with no arguments. It can be passed as an argument in the constructor call.
It can be used with instance members. It is used to return the current class instance from the method.

Article Tags :