Bird
0
0

What is the output of the following code?

medium📝 Predict Output Q13 of 15
Java - Polymorphism
What is the output of the following code?
class Parent {
    void show() { System.out.println("Parent"); }
}
class Child extends Parent {
    @Override
    void show() { System.out.println("Child"); }
}
public class Test {
    public static void main(String[] args) {
        Parent obj = new Child();
        obj.show();
    }
}
AParent
BRuntime error
CCompilation error
DChild
Step-by-Step Solution
Solution:
  1. Step 1: Understand dynamic method dispatch

    When a parent reference points to a child object, the overridden child method is called at runtime.
  2. Step 2: Check method overriding and call

    The show() method is overridden in Child, so obj.show() calls Child's version.
  3. Final Answer:

    Child -> Option D
  4. Quick Check:

    Overridden method called at runtime = B [OK]
Quick Trick: Overridden method runs, not parent, when using child object [OK]
Common Mistakes:
  • Expecting parent method output
  • Confusing compile-time and runtime method calls
  • Ignoring @Override annotation effect

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes