Assume a class, OOFClass, defines a constructor with no arguments. You are editing OOFClass, and you want to create a new constructor that reuses the existing constructor to do some of the work. How would you call that constructor?
Select one:
a. super(), called anywhere in the old constructor
b. this(), called anywhere in the old constructor
c. super(), called as the first command in the new constructor
d. super(), called as the first command in the old constructor
e. this(), called anywhere in the new constructor
f. this(), called as the first command in the old constructor
g. super(), called anywhere in the new constructor
h. this(), called as the first command in the new constructor
Answer is:
h. this(), called as the first command in the new constructor.
This is clearly infered from the following example code in java:-
class OOF
{
// default constructor 1
OOF()
{
System.out.println("default constructor");
}
// parameterized constructor 1 (new constructor)
OOF(int x)
{
// invokes default constructor
this();
System.out.println(x);
}
public static void main(String args[])
{
// invokes parameterized constructor 2
new OOF(100);
}
}
And hence it prints output as:-
default constructor
100
Note:-
Get Answers For Free
Most questions answered within 1 hours.