Runnable contract — which method signature must a class provide to correctly implement java.lang.Runnable?
-
Avoid run()
-
Bpublic void run()
-
Cpublic void start()
-
Dvoid run(int priority)
-
Eprotected void run()
Answer
Correct Answer: public void run()
Explanation
Introduction / Context:The Runnable interface defines the entry point executed by a Thread. Implementing classes must adhere to the exact method signature and visibility expected by the interface.
Given Data / Assumptions:
- Interface declaration is
public interface Runnable { public abstract void run(); } - Implementers must not reduce visibility on implemented methods.
Concept / Approach:Since interface methods are implicitly public abstract, an implementing method must be declared public. Omitting public (package-private) would reduce visibility and cause a compile-time error.
Step-by-Step Solution:
Required signature:public void run().Optional modifiers like @Override may be used but do not change the required visibility and signature.Verification / Alternative check:Try compiling a class with void run() (no public); the compiler reports that the method attempts to assign weaker access privileges than the interface method.
Why Other Options Are Wrong:void run() (no public) has insufficient visibility; start() is a Thread method, not part of Runnable; overloads like run(int) do not satisfy the interface.
Common Pitfalls:Forgetting to declare public or misspelling the signature.
Final Answer:public void run()