Menu
Topics Index
...
`

Chaining of constructors


If you need explanation Read this topic

If you need Answer Take test on this topic

Chaining of constructors
What will be the output of the following program?
public class Chaining
{
    public Chaining()
    {
        System.out.println("Default constructor");
    }
    public Chaining(int i)
    {
        this();
        System.out.println("Single parameter constructor: " + i);
    }
    public Chaining(int i, int j)
    {
        this(j);
        System.out.println("Double parameter constructor: " + i + ", " + j);
    }
    public static void main(String a[])
    {
        Chaining ch = new Chaining(0, 2);
    }
}

A. Default constructor
Single parameter constructor: 2
Double parameter constructor: 0, 2
B. Default constructor
Single parameter constructor: 0
Double parameter constructor: 0, 2
C. Default constructor
Single parameter constructor: 2
D. Default constructor
Double parameter constructor: 0, 2
E. Compilation Error or Runtime Error
Topic: this Keyword In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

answer is {A} as first we are calling two peramiter constroctor and in that using this(j) we are calling one parameter contructor and there we are calling zero parameter contructor using this().

Posted by Ashok Kumar Paritala    2014-06-16 08:40:57


Ans:A  constructor will be done here. first deault,1-param,2-param

Posted by Indrasena Reddy    2014-06-16 08:44:04


Answer  : A
first the Default Constructor will get executed then the DoubleParameter constructor is being executd which inturn will call the SingleParameter constructor before printing the values 0 ,2

Posted by Deepak Kumar Yadav    2014-06-16 08:44:52


ans a. 1st default constructor executd and prind.
2nd  this() is used to invok overloaded constructor.this takes j value from 2 parametrd constructor and executes current constructr(single parametr),
3rd line prints calld 2parameter constructr.

Posted by Maheshwari Natarajan    2014-06-16 09:06:36


ans a. 1st default constructor executd and prind.
2nd this() is used to invok overloaded constructor

Posted by Vijaya Vijaya    2014-06-17 05:31:29


Congratulations Ashok Kumar Paritala. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-06-18 03:59:59


© meritcampus 2019

All Rights Reserved.

Open In App