Difficulty: Easy
Correct Answer: Compilation fails.
Explanation:
Introduction / Context:
This question targets the rule that methods declared final cannot be overridden in subclasses. It asks what happens when a subclass B declares a method with the same signature as a final method in superclass A.
Given Data / Assumptions:
Concept / Approach:
In Java, the final modifier on an instance method prevents overriding in any subclass. Declaring a method in B with the exact same signature attempts to override the final method and violates the language rule, causing a compile-time error. The program never runs.
Step-by-Step Solution:
Verification / Alternative check:
Change B’s method name or remove final in A; compilation would then succeed, producing output based on the chosen implementation.
Why Other Options Are Wrong:
Common Pitfalls:
Confusing final with static or private: private methods are not overridden but hidden; final prohibits overriding.
Final Answer:
Compilation fails.
Discussion & Comments