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.
public class Constructor_Chaining
{
public Constructor_Chaining
{
System.out.println("In default constructor");
}
public Constructor_Chaining(int i)
{
this();
System.out.println("In single parameter constructor");
}
public Constructor_Chaining(int i,int j)
{
this(j);
System.out.println("In double parameter constructor");
}
public static void main(String a[])
{
Constructor_Chaining obj = new Constructor_Chaining(11,12);
}
}
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
Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.
Related Posts:
- Apply for Computer Science Internship
- Apply for Java Internship
- Check Java Books
- Practice Information Technology MCQs
- Check Programming Books