Constructor Chaining In Java programming


The constructor chaining is a particular sequence of injecting constructors when an user initialize an object in a particular method. This process can be used when we invoke a bulk number of constructors one by one only on the basis of the instance class. This process is an another method linked with the inheritance where the task of a sub-class constructor to call a super class constructor.

Constructor chaining can be performed in two ways in Java −

  • Within same class − The process can be done by the using of this() keyword for the constructors present in the same class.

  • From base class − by using the super() keyword, we can call the constructor class from the base class.

Here is an example −

ConstructorChaining(int x){
	System.out.println("Parameterized (First Parameter) Constructor Of
	Class.");
	System.out.println("The Value Of x Is "+x);
}
ConstructorChaining(int x, int y){
	this();
	System.out.println("Parameterized (Second Parameters) Constructor Of
	The Class.");
	System.out.println("The Value Of x And y Is " + x + "And " + y + ". "
	+ "The Sum Of x And y Is " + (x + y) );
}

Algorithm to Perform Constructor Chaining in Java

In this possible algorithm, we are going to show you how to perform the constructor chaining process in a Java environment. By using this possible algorithm we will build some syntax, by which we will be able to describe the problem statement with various approaches.

  • Step 1 − Start the process.

  • Step 2 − Declare the process package.

  • Step 3 − Import and utilise the Java packages to run the process.

  • Step 4 − Declare a public class.

  • Step 5 − Declare the first default constructor.

  • Step 6 − This constructor will call an another constructor from the same class.

  • Step 7 − Call an another constructor.

  • Step 8 − Call a parameterized constructor as a process.

  • Step 9 − Declare a this() operator.

  • Step 10 − Go for the declaration of another parameterized constructor as a pair.

  • Step 11 − Mention the print method.

  • Step 12 − Declare a string argument.

  • Step 13 − Invoke the default constructor first.

  • Step 14 − Call the No-argument constructor for the further.

  • Step 15 − Get the value of Derived obj = new Derived().

  • Step 16 − Get the return value and terminate the process.

Syntax to Perform Constructor Chaining in Java

class A {
	public int a;
	public A() {
		this(-1);
	}
	public A(int a) {
		this.a = a;
	}
	public String toString() {
		return "[ a= " + this.a + "]";
	}
}
class B extends A {
	public int b;
	public B() {
		this(-1,-1);
	}
	public B(int a, int b) {
		super(a);
		this.b = b;
	}
	public String toString() {
		return "[ a= " + this.a + ", b = " + b + "]";
	}
}
public class Tester {
	public static void main(String args[]) {
		A a = new A(10);
		System.out.println(a);
		B b = new B(11,12);
		System.out.println(b);
		A a1 = new A();
		System.out.println(a1);
		B b1 = new B();
		System.out.println(b1);
	}
}

In this possible syntax above, we have tried to show you how to create and perform a constructor chaining method in a Java environment. By using these syntax mentioned above, we are heading towards some possible approces to solve the problem statement in an efficient manner.

Approaches to Follow

  • Approach 1 − Java program to illustrate the constructor chaining within same class Using this(), super(), Init block keyword

  • Approach 2 − Java program to build a logic where one constructor is calling another constructor using this keyword as well as operate on an object

Approach 1: to Illustrate the Constructor Chaining Within Same Class Using This(), Super(), Init Block Keyword

Use of This() Method

In this possible approach, we are going to apply the this() method to illustrate the constructor chaining within same class.

public Person(String firstName, String middleName, String lastName, int age) {
	this.firstName = firstName;
	this.middleName = middleName;
	this.lastName = lastName;
	this.age = age;
}

Example

//Java program to illustrate the constructor chaining within same class Using this() keyword
public class ARBRDD{
   ARBRDD(){
      this(7);
      System.out.println("The Default Constructor Value Is Here!");
   }
   ARBRDD(int x){
      this(7, 16);
      System.out.println(x);
   }
   ARBRDD(int x, int y){
      System.out.println(x * y);
   }
   public static void main(String args[]){
      new ARBRDD();
   }
}

Output

112
7
The Default Constructor Value Is Here!

Use of Constructor Order Swap Method

In this possible approach, we are going to apply this() keyword to illustrate Constructor Chaining within same class. By this we can also deploy the changing order of constructors.

Example

//Java program to illustrate Constructor Chaining within same class Using this() keyword and also deploy the changing order of constructors
public class ARBRDD{
   ARBRDD(){
      System.out.println("Default Value Is Here!");
   }
   ARBRDD(int x){
      this();
      System.out.println(x);
   }
   ARBRDD(int x, int y){
      this(5);
      System.out.println(x * y);
   }
   public static void main(String args[]){
      new ARBRDD(7, 16);
   }
}

Output

Default Value Is Here!
5
112

Use of Super() Method

In this possible approach, we are going to apply a super() class to illustrate Constructor Chaining to other class.

Example

//Java program to illustrate Constructor Chaining to other class using super() keyword
class ARBRDD{
   String name;
   ARBRDD(){
      this("");
      System.out.println("No-argument Constructor Of The" + " Base Class Is Here");
   }
   ARBRDD(String name){
      this.name = name;
      System.out.println("Calling The Parameterized Constructor" + " Of The Base Class>>");
   }
}
public class Derived extends ARBRDD{
   Derived(){
      System.out.println("No-argument Constructor Value " +
      "Of Derived Here");
   }
   Derived(String name){
      super(name);
      System.out.println("Calling The Parameterized " +
      "Constructor Of Derived Value!!");
   }
   public static void main(String args[]){
      Derived obj = new Derived("TEST");
   }
}

Output

Calling The Parameterized Constructor Of The Base Class>>
Calling The Parameterized Constructor Of Derived Value!!

Use of Init Block Method

In this possible approach, we are going to apply the Init block method, by which the common resources to be executed with the every constructor value.

Example

//Java program to demonstrate using Init block where common resources to be executed with the every constructor value
public class ARBRDD{{
      System.out.println("Init Block Value Is Here Now!");
   }
   ARBRDD(){
      System.out.println("Default Value Displaying Now!!");
   }
   ARBRDD(int x){
      System.out.println(x);
   }
   public static void main(String[] args){
      new ARBRDD();
      new ARBRDD(071610);
   }
}

Output

Init Block Value Is Here Now!
Default Value Displaying Now!!
Init Block Value Is Here Now!
29576

Use of Blockage Removing Method

In this possible approach, we are going to apply the blockage method to perform the removing elements from the blockage after the first block occurrence.

Example

//Java program to perform the removing the blockage to be executed after the first block
public class ARBRDD{{
      System.out.println("Init Value");
   }
   ARBRDD(){
      System.out.println("Default Value Is Here");
   }
   ARBRDD(int x){
      System.out.println(x);
   }{
      System.out.println("Second Value Is Here Now!");
   }
   public static void main(String args[]){
      new ARBRDD();
      new ARBRDD(071610);
   }
}

Output

Init Value
Second Value Is Here Now!
Default Value Is Here
Init Value
Second Value Is Here Now!
29576

Approach 2: Build a Logic Where one Constructor is Calling Another Constructor

Use of the Another Constructor Calling Method

In this possible approach,we are going to apply a logic where one constructor is calling another constructor using this keyword to build an employee record management system.

public Customer(String firstName, String lastName, int age, String CardId) {
   this(firstName, null, lastName, age, CardId);
}
public Customer(String firstName, String middleName, String lastName, int age,
String CardId) {
   super(firstName, middleName, lastName, age);
   this.CardId = CardId;
}

Example 1

//Java program to build a logic where one constructor is calling another constructor using this keyword.
public class Employee{
   public String empName;
   public int empSalary;
   public String address;
   public Employee(){
      this("ARB");
   }
   public Employee(String name){
      this(name, 07102001);
   }
   public Employee(String name, int sal){
      this(name, sal, "DINAJPUR");
   }
   public Employee(String name, int sal, String addr){
      this.empName=name;
      this.empSalary=sal;
      this.address=addr;
   }
   void disp(){
      System.out.println("Employee Name Is: "+empName);
      System.out.println("Employee Total Salary Is: "+empSalary);
      System.out.println("Employee Parmanent Address Is: "+address);
   }
   public static void main(String[] args){
      Employee obj = new Employee();
      obj.disp();
   }
}

Output

Employee Name Is: ARB
Employee Total Salary Is: 1868801
Employee Parmanent Address Is: DINAJPUR

Example 2

//Java code to declare a constructor without arguments which is invoked when we create an object and perform the constructor chaining
class Constructer{
   Constructer(){
      System.out.println("Constructer With The Zero Value Arguments");
   }
   Constructer(int x){
      System.out.println("Constructer With The Argument One Type Int Value "+"("+x +")");
   }
   Constructer(double x){
      System.out.println("Constructer With The Argument Of The One Type Double Value"+"("+x +")");
   }
   Constructer(int x,int y){
      System.out.println("Constructer With Two Arguments Of The Type Int"+"("+x+","+y+")");
   }
   Constructer(double x,double y){
      System.out.println("Constructer With Two Argument Type Double"+"("+x+","+y+")");
   }
   Constructer(double x,int y){
      System.out.println("Constructer With Two Arguments Type Double and Int Value "+"("+x+","+y+")");
   }
   Constructer(int x,double y){
      System.out.println("Constructer With Two Arguments Type Int and Double"+"("+x+","+y+")");
   }
   Constructer(int x,int y,double z){
      System.out.println("Constructer With Three Arguments Two Of Type Int and One Is Double"+"("+x+","+y+","+z+")");
   }
}
public class ARBRDD{
   public static void main(String args[]){
      Constructer c1=new Constructer();
      Constructer c2=new Constructer(1);
      Constructer c3=new Constructer(1.0);
      Constructer c4=new Constructer(2,3);
      Constructer c5=new Constructer(3.4,4.5);
      Constructer c6=new Constructer(1.2,2);
      Constructer c7=new Constructer(2,3.2);
      Constructer c8=new Constructer(1,2,3);
   }
}

Output

Constructer With The Zero Value Arguments
Constructer With The Argument One Type Int Value (1)
Constructer With The Argument Of The One Type Double Value(1.0)
Constructer With Two Arguments Of The Type Int(2,3)
Constructer With Two Argument Type Double(3.4,4.5)
Constructer With Two Arguments Type Double and Int Value (1.2,2)
Constructer With Two Arguments Type Int and Double(2,3.2)
Constructer With Three Arguments Two Of Type Int and One Is Double(1,2,3.0)

Conclusion

In the field of Java programming, the constructor chaining is process of calling one constructor from another constructor. In this article today, we have learned about the constructor chaining method. By using the above mentioned syntax and algorithm we have built some Java codes to explain the problem statement in an efficient manner.

Updated on: 29-Dec-2023

547 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements