Java Program to Illustrate Use of Chaining Constructor

This is a Java Program to illustrate the use of chaining of constructor.

Calling of one constructor from another constructor with respect to current object is constructor chaining. It can be achieved in any order.

Here is the source code of the Java Program to illustrate the use of chaining of constructor. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. public class Constructor_Chaining
  2. {
  3.         public Constructor_Chaining
  4.         {
  5.             System.out.println("In default constructor");
  6.         }
  7.         public Constructor_Chaining(int i)
  8.         {
  9.             this();
  10.             System.out.println("In single parameter constructor");
  11.         }
  12.         public Constructor_Chaining(int i,int j)
  13.         {
  14.             this(j);
  15.             System.out.println("In double parameter constructor");
  16.         }
  17.         public static void main(String a[])
  18. 		{
  19.            Constructor_Chaining obj = new Constructor_Chaining(11,12);
  20.         }
  21. }

Output:

$ javac Constructor_Chaining.java
$ java Constructor_Chaining
 
In default constructor
In single parameter constructor
In double parameter constructor

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.