Bird
0
0

Find the error in this code related to runtime polymorphism:

medium📝 Debug Q14 of 15
Java - Polymorphism
Find the error in this code related to runtime polymorphism:
class Parent {
  void show() { System.out.println("Parent"); }
}
class Child extends Parent {
  void show(int x) { System.out.println("Child " + x); }
}
public class Test {
  public static void main(String[] args) {
    Parent p = new Child();
    p.show();
  }
}
AParent class method show() is private
BChild class does not override show() method correctly
CCannot assign Child object to Parent reference
DMissing main method
Step-by-Step Solution
Solution:
  1. Step 1: Check method overriding in Child class

    Child defines show(int x), which is overloading, not overriding show().
  2. Step 2: Understand method call on Parent reference

    Parent reference calls show() with no arguments, but Child has no overriding method, so Parent's method runs.
  3. Final Answer:

    Child class does not override show() method correctly -> Option B
  4. Quick Check:

    Overriding needs exact method signature match [OK]
Quick Trick: Overriding needs same method signature, not just same name [OK]
Common Mistakes:
  • Thinking overloading is overriding
  • Expecting Child's show(int) to override show()
  • Ignoring method parameters in overriding

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes