Java SE 11 Developer — Question 4
Given:
public class X {
}
and
public final class Y extends X {
}
What is the result of compiling these two classes?
Answer options
- A. The compilation fails because there is no zero args constructor defined in class X.
- B. The compilation fails because either class X or class Y needs to implement the toString() method.
- C. The compilation fails because a final class cannot extend another class.
- D. The compilation succeeds.
Correct answer: B
Explanation
The correct answer is B because in Java, every class must either directly or indirectly implement the toString() method if it is extended, which is not done here. Option A is incorrect because Java provides a default no-argument constructor for classes that do not define any constructor. Option C is wrong as a final class can extend another class; it just cannot be subclassed itself. Option D is false because the compilation does not succeed due to the missing toString() method implementation.