Difficulty: Medium
Correct Answer: 3 and 4
Explanation:
Introduction / Context:
This question evaluates understanding of how classes implement interfaces, how interfaces extend other interfaces, and how abstract classes can omit implementations of interface methods.
Given Data / Assumptions:
Base
declares two methods: m1()
and m2(short)
.
Concept / Approach:
Key rules: (1) Interfaces extend interfaces; they do not implement them. (2) Classes implement interfaces (not extend) and must provide public implementations for interface methods unless the class is abstract. (3) An implementing method cannot reduce visibility.
Step-by-Step Solution:
interface Base2 implements Base
→ invalid; interfaces must use extends
.Fragment 2: abstract class ... extends Base
→ invalid; classes cannot extend an interface; also only one method provided.Fragment 3: abstract class ... implements Base
with no bodies → valid; abstract class may defer implementation.Fragment 4: Provides a public
body for m1()
and can remain abstract because m2
is unimplemented → valid.Fragment 5: Uses protected
for m1()
; visibility is weaker than the interface's public
contract → invalid.
Verification / Alternative check:
Compiling minimal classes with each fragment confirms that only 3 and 4 succeed.
Why Other Options Are Wrong:
They rely on incorrect keywords, inheritance relationships, or visibility modifiers.
Common Pitfalls:
Confusing extends
vs. implements
; forgetting that interface methods are implicitly public abstract
.
Final Answer:
3 and 4
Discussion & Comments