Bird
0
0

What is the output of this code?

medium📝 Predict Output Q4 of 15
Java - Interfaces
What is the output of this code?
interface A { void show(); }
class B implements A {
public void show() { System.out.println("Hello"); }
}
public class Test {
public static void main(String[] args) {
A obj = new B();
obj.show();
}
}
AHello
BCompilation error
CRuntime error
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Analyze interface and class implementation

    Class B implements interface A and provides the show() method printing "Hello".
  2. Step 2: Check main method execution

    Object obj of type A refers to new B(), calling show() prints "Hello".
  3. Final Answer:

    Hello -> Option A
  4. Quick Check:

    Interface method call prints implemented output [OK]
Quick Trick: Interface reference can call implemented methods [OK]
Common Mistakes:
  • Forgetting to make show() public
  • Expecting interface to print directly
  • Confusing compile and runtime errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes