Bird
0
0

Find the error in this code snippet:

medium📝 Debug Q7 of 15
Java - Interfaces
Find the error in this code snippet:
interface I {
    default void foo() {
        bar();
    }
    void bar();
}
class C implements I {
    public void bar() {
        System.out.println("bar");
    }
}
AInterface default method cannot call abstract methods.
BClass C must implement foo() method.
CInterface methods cannot be default and abstract together.
DNo error; code compiles and runs fine.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze default method calling abstract method

    Default method foo() calls abstract method bar(), which is allowed.
  2. Step 2: Check class implementation

    Class C implements bar(), so no error occurs.
  3. Final Answer:

    No error; code compiles and runs fine. -> Option D
  4. Quick Check:

    Default method can call abstract method if implemented later [OK]
Quick Trick: Default methods can call abstract interface methods [OK]
Common Mistakes:
  • Assuming default methods cannot call abstract methods
  • Expecting class to implement default methods

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes