Bird
0
0

Find the error in this code snippet:

medium📝 Debug Q14 of 15
Java - Interfaces
Find the error in this code snippet:
interface Shape {
void draw();
}
class Circle implements Shape {
void draw() { System.out.println("Circle drawn"); }
}
AInterface cannot have methods
BNo error, code is correct
CCircle class should be abstract
DMethod draw() must be public in Circle class
Step-by-Step Solution
Solution:
  1. Step 1: Check method visibility rules

    Interface methods are implicitly public, so implementing methods must be public too.
  2. Step 2: Identify method visibility in Circle

    Method draw() in Circle has default (package-private) visibility, missing public.
  3. Final Answer:

    Method draw() must be public in Circle class -> Option D
  4. Quick Check:

    Interface methods require public implementation = B [OK]
Quick Trick: Implement interface methods as public always [OK]
Common Mistakes:
  • Ignoring method visibility mismatch
  • Thinking interface methods can be private
  • Assuming no error if method is package-private

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes