Bird
0
0

Identify the error in this code snippet:

medium📝 Debug Q6 of 15
Java - Interfaces
Identify the error in this code snippet:
interface Sample {
    static void print() {
        System.out.println("Hello");
    }
}
class Test implements Sample {
    public void print() {
        System.out.println("Test");
    }
}
ANo error, code is correct
BCannot override static method from interface
CInterface static methods cannot have body
DMissing static keyword in class method
Step-by-Step Solution
Solution:
  1. Step 1: Verify interface static method

    The static method 'print' in Sample has a required body and correct syntax.
  2. Step 2: Analyze class method

    Class Test defines an instance method 'print()', which does not conflict with or override the interface's static method, as static methods belong to the interface and are not inherited for overriding.
  3. Final Answer:

    No error, code is correct -> Option A
  4. Quick Check:

    Static interface + instance class same name = no conflict [OK]
Quick Trick: Static interface methods cannot be overridden [OK]
Common Mistakes:
  • Trying to override static interface methods
  • Confusing static and instance methods
  • Ignoring method signatures

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes