In Java, constructor calls are made using both super() and this(). this() is used to call the constructor of the current class, while super() is used to call the constructor of the base class in Java (i.e., Parent’s class).
Let’s take a closer look at both of them:
super() Function in Java
super(): The constructor of the Base class(Parent class) is called using super().
Example
class Parents { Parents() { System.out.println("Parent class's No " + " arg constructor"); } } public class Childs extends Parents { Childs() { super(); System.out.println("Flow comes back from " + "Parent class no arg const"); } public static void main(String[] args) { new Childs(); System.out.println("Inside Main"); } }
Program Flow
We’ve added a new Child() statement to the main, which calls the Child class’s no argument constructor.
We have super() inside that, which calls Parent class’s no argument constructor because we have written super() and no arguments, which calls Parent class’s no argument constructor. We also have an SOP statement, so it prints the Parent class’s No arg constructor.
As the Parent class’s No argument const completes, flow returns to the Child class’s no argument, where we have an SOP statement, and therefore it outputs Flow returns from Parent class’s no arg const.
After finishing the no argument constructor of the child class, the flow returned to main and executed the remaining statements, and printed them inside Main.
We can only use super() inside constructors, not in static contexts or inside methods, and super() should be the first statement inside the constructor.
Example
class Parents { Parents() { System.out.println("Parent class's No " + "arg constructor"); } } public class Childs extends Parents { Childs() { super(); System.out.println("Flow comes back from " + "Parent class no arg const"); } public static void main(String[] args) { new Childs(); System.out.println("Inside main"); } }
Super() should always be the initial statement in any constructor in Java. It can only be used within the constructor and not anywhere else. Only the constructor of the parent class (super class) is referred to with super().
this() Function in Java
this() is used to invoke the constructor of the current class.
Example
public class PP { PP() { this(20); System.out.println("Flow comes back from " + "PP class's 1 arg const"); } PP(int c) { System.out.println("PP class's 1 arg const"); } public static void main(String[] args) { new PP(); System.out.println("Inside Main"); } }
Program Flow
Start with main, and then add new Child(), which calls the no-argument constructor of the Child class, followed by this(20), which calls the current class’s single argument constructor (i.e., PP class)
Because we wrote this(20) with just one argument, the constructor of the PP class is called with only one argument. There is an SOP statement in that, and as a result, the 1 arg const of the PP class is printed.
As the 1 argument const of the PP class completes, flow returns to the no argument of the PP class, where we have an SOP statement, and so it outputs Flow returns from the 1 arg const of the PP class.
After finishing the PP class’s no-argument constructor, the flow returned to the main and executed the remaining statements and prints inside Main.
this() can only be used inside the constructor and nowhere else, not even in a static context, and it should be the first statement inside the constructor.
Example
public class PP { PP() { this(61); System.out.println("Flow comes back from PP " + "class 1 arg const"); } PP(int h) { System.out.println("PP class's 1 arg const"); } public static void main(String[] args) { new PP(); System.out.println("Inside main"); } }
Note that the first statement in any constructor should be this(). It can only be used within the constructor and not anywhere else. This() only refers to the constructor of the current class.
this() and super() are important concepts to understand!
We can only use super() and this() once inside the constructor. If we use super() twice, this() twice, or super() followed by this() or this() followed by super(), we get a compile time error (). We can only use super() or this() as the initial statement inside the constructor, not both.
It is entirely up to us whether or not to use super() or this() because if neither this() nor super() is used, the compiler will default to using super() as the first statement inside the constructor.
Example
class Parents { Parents() { System.out.println("Parent class's No " + "argument constructor"); } Parents(int x) { System.out.println("Parent class's 1 argument" + " constructor"); } } public class Bases extends Parents { Bases() { System.out.println("Base class's No " + "argument constructor"); } public static void main(String[] args) { new Bases(); System.out.println("Inside Main"); } }
Program Flow
We have new Bases() inside the main, and then the flow goes to the Base class’s No argument constructor.
After that, if we don’t use either super() or this(), the compiler will use super by default ().
As a result, flow is directed to the Parent class’s No arg constructor rather than the 1 argument constructor.
The No argument constructor of the Parent class is then printed.
When the Parent() constructor is finished, the flow returns to the Base class’s No argument constructor and performs the next SOP statement, the Base class’s No argument constructor.
After finishing that, the No argument constructor flow returns to main() and prints the remaining statements within main(), i.e., Inside main.
We can use this() before super if we specify it explicitly.
class Parents { Parents() { System.out.println("Parent class's No " + "argument constructor"); } Parents(int x) { System.out.println("Parent class's one " + " argument constructor"); } } public class Bases extends Parents { Bases() { this(20); System.out.println("No arg const"); } Bases(int x) { this(20, 30); System.out.println("1 arg const"); } Bases(int c, int d) { System.out.println("2 arg const"); } public static void main(String[] args) { new Bases(); System.out.println("Inside Main"); } }
The use of a recursive constructor is not allowed.
class PP { PP() { this(40); } PP(int x) { this(); } public static void main(String[] args) { new PP(); } }
Program Flow
The flow starts with main() and then moves to the PP class’s No arg constructor. After that, we have this(40), and flow goes to the 1 arg constructor of PP, and in that, we have this(), so flow goes to the No arg constructor of the base class, and in that, we have this(40), and flow goes to the 1 arg constructor of Base class, and so on, in a recursive manner. As a result, we get a build time error that says repeated constructor invocation. In Java, recursive constructor invocations are not permitted.
As we can see, both super() and this() are important yet different methods in Java.