Given the following class definition which of the following can be legally placed after the comment line?
package net.dharmaraj;
class Base
{
public Base(int i)
{
}
}
public class Derived extends Base
{
public static void main(String arg[])
{
Derived d = new Derived(10);
}
Derived(int i)
{
super(i);
}
Derived(String s, int i)
{
this(i);
// Here
}
}
A) Derived d = new Derived();
B) super();
C) this("Hello",10);
D) Base b = new Base(10);
Answer:--
D) Base b = new Base(10);
Explanation:
A) This is wrong because there is no matching constructor defined in Derived class.
B) The super keyword suppose to be the first line in the constructor.
C) The this keyword suppose to be first line in the constructor.
D) This is correct because there is matching constructor in Base class.
No comments:
Post a Comment