Bird
0
0

What will be the output of the following code?

medium📝 Predict Output Q4 of 15
Java - Interfaces
What will be the output of the following code?
interface I {
    default void print() {
        System.out.println("Interface I");
    }
}
class A implements I {
    public void print() {
        System.out.println("Class A");
    }
}
public class Test {
    public static void main(String[] args) {
        I obj = new A();
        obj.print();
    }
}
AInterface I
BCompilation error
CClass A
DRuntime exception
Step-by-Step Solution
Solution:
  1. Step 1: Understand method overriding

    Class A overrides the default method print() from interface I.
  2. Step 2: Determine method called at runtime

    Since obj is an instance of A, overridden print() in A is called, printing "Class A".
  3. Final Answer:

    Class A -> Option C
  4. Quick Check:

    Overridden method called = Class A [OK]
Quick Trick: Overridden methods in class override default interface methods [OK]
Common Mistakes:
  • Expecting default method output instead of overridden
  • Confusing interface reference type with runtime object

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes