Bird
0
0

Examine the following code snippet:

medium📝 Debug Q6 of 15
Java - Interfaces
Examine the following code snippet:
interface Runner { void run(); }
class Athlete implements Runner {
void run() { System.out.println("Athlete running"); }
}

What is the issue with this code?
AThe method run() should return a value
BThe interface Runner cannot be implemented by Athlete
CThe run() method must be declared public in Athlete class
DThe class Athlete should be abstract
Step-by-Step Solution
Solution:
  1. Step 1: Check interface method visibility

    Methods in interfaces are implicitly public and abstract.
  2. Step 2: Verify method implementation in class

    When implementing interface methods, the method must be declared public to maintain visibility.
  3. Step 3: Identify the error

    The method run() in Athlete is package-private (default) and should be public.
  4. Final Answer:

    The run() method must be declared public in Athlete class -> Option C
  5. Quick Check:

    Interface methods must be public in implementing classes [OK]
Quick Trick: Interface methods must be public when implemented [OK]
Common Mistakes:
  • Not declaring implemented methods as public
  • Assuming interface methods can have default visibility
  • Thinking interface methods require return values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes