Bird
0
0

Identify the error in the following code:

medium📝 Debug Q6 of 15
Java - Interfaces
Identify the error in the following code:
interface A {
    default void display() {
        System.out.println("A");
    }
}
class B implements A {
    void display() {
        System.out.println("B");
    }
}
AInterface A cannot have default methods.
BDefault methods cannot be overridden.
CMethod display() in class B must be public.
DClass B must be declared abstract.
Step-by-Step Solution
Solution:
  1. Step 1: Check method overriding rules

    Methods overriding interface methods must have the same or broader access modifier.
  2. Step 2: Identify access modifier mismatch

    Interface methods are public by default; class B's display() is package-private, causing error.
  3. Final Answer:

    Method display() in class B must be public. -> Option C
  4. Quick Check:

    Overriding method access = Must be public [OK]
Quick Trick: Override interface methods with public access [OK]
Common Mistakes:
  • Using default (package) access instead of public
  • Thinking default methods cannot be overridden

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes