Bird
0
0

Identify the error in this interface declaration:

medium📝 Debug Q14 of 15
Java - Interfaces
Identify the error in this interface declaration:
interface Calculator {
    int add(int a, int b) {
        return a + b;
    }
}
AInterfaces cannot have method bodies unless default or static
BMethod name 'add' is invalid in interfaces
CReturn type 'int' is not allowed in interfaces
DInterface name must start with lowercase
Step-by-Step Solution
Solution:
  1. Step 1: Check method body rules in interfaces

    In Java, interface methods cannot have bodies unless marked as default or static.
  2. Step 2: Analyze the given method

    The method add has a body but no default or static keyword, causing a syntax error.
  3. Final Answer:

    Interfaces cannot have method bodies unless default or static -> Option A
  4. Quick Check:

    Method bodies in interfaces need default/static [OK]
Quick Trick: Interface methods need default/static for bodies [OK]
Common Mistakes:
  • Adding method bodies without default/static
  • Thinking method names are restricted
  • Ignoring Java naming conventions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes