How to call one constructor from another in Java

In this tutorial, we will learn how to call one constructor from another in Java. This program will help you to develop more knowledge about constructors, this() and super().

First of all, we should know that the process of calling one constructor from another constructor concerning the same object is known as constructor chaining.

Why Constructor chaining?

  • Constructor chaining is for code maintenance. It increases program readability and usability.
  • The called constructor also refers to the same object. Therefore, no new object gets created.
  • Since we know that at the time of object creation, only one constructor can be called. In such scenarios, constructor chaining plays a significant role.

How to call one constructor from another in java

To achieve our goal we have 2 ways :

  1. By using this() keyword: which is used for calling the same class constructor.
  2. By using super() keyword: which is used for calling the Base class constructor.

let’s understand them one by one:

1.By using this() keyword. It is used to call the constructor of the same class. this() must be the first statement in the constructor. let’s have a look at the java code:

public class Main
{
  Main()					       //constructor-1 of Main class
  {	
    this("need some code?");		               //called constructor-2 
    System.out.println("find at codespeedy!");
  }
  
  Main(String s)			               //constructor-2 of Main class
  {
    System.out.println(s);
  }
  
  public static void main(String []args)
  {
    Main a=new Main();				       //called constructor-1
  }
}

output:




need some code?
find at codespeedy!

2.By using super() keyword. It is used to call the constructor of the base class. Like this(), super() must also be the first statement in the constructor.let’s have a look at the Java code:

class Base
{
  Base()
  {
    System.out.println("default constructor of base class");
  }
  
}
public class Main extends Base  //extends is a keyword used to inherit a class
{
  
  Main()                        //constructor-1 of Main class
  {	
    this("need some code?");   //called constructor-2 
    System.out.println("find at codespeedy!");
  }
  
  Main(String s)               //constructor-2 of Main class
  {
    super();
    System.out.println(s);
  }
  
  public static void main(String []args)
  {
    Main a=new Main();        //called constructor-1
  }
}

output:

default constructor of base class
need some code?
find at codespeedy!

Explanation:

first of all, main() function creates an object by calling constructor-1 which calls constructor-2 using this() keyword. From where it calls the Base class’s constructor using super().

We hope through this article we solve all your queries, share your feedback in the comment section.

you may also read:

difference between constructor and method in Java

Concept Of Class and Object in Java With Examples


Leave a Reply

Your email address will not be published. Required fields are marked *