Difficulty: Medium
Correct Answer: The compiler reports an error because in Java a class must extend a superclass before it implements interfaces in the header.
Explanation:
Introduction / Context:
Java has a strict syntax for class declarations that combine inheritance and interface implementation. Interview questions often test whether candidates know the correct ordering of extends and implements in a class header and what happens when that order is violated, as in the expression \"X implements Y extends Z\".
Given Data / Assumptions:
Concept / Approach:
The valid form of a Java class header that uses both inheritance and interfaces is:
class X extends Z implements Y1, Y2, ...
First the class may extend a single superclass using the extends keyword. After that, the class may implement one or more interfaces using the implements keyword. Reversing the order to \"implements ... extends ...\" does not match the Java grammar, so the compiler cannot parse it as a valid declaration and must report a compilation error.
Step-by-Step Solution:
Verification / Alternative check:
If a developer copies such a header into a Java source file and attempts to compile it with javac, the compiler issues an error message indicating a syntax problem near the implements or extends keyword. Changing the order to \"extends Z implements Y\" immediately resolves the error, confirming that ordering is mandatory.
Why Other Options Are Wrong:
Common Pitfalls:
A common mistake is to assume that the compiler may reorder extends and implements automatically, which it does not. Another pitfall is confusing the syntax rules for classes with those for interfaces, where multiple inheritance of interfaces is allowed but still uses a consistent grammar.
Final Answer:
The correct choice is The compiler reports an error because in Java a class must extend a superclass before it implements interfaces in the header. because it reflects the strict ordering rules of the Java class declaration syntax.
Discussion & Comments