Bird
0
0

Identify the error in the following code snippet:

medium📝 Debug Q14 of 15
Java - Polymorphism
Identify the error in the following code snippet:
class Parent {
    void display() {}
}
class Child extends Parent {
    @Override
    void display(int x) {}
}
ACannot use @Override annotation on any method.
BMissing return type in Child's display method.
CMethod display(int x) does not override display() due to different parameters.
DChild class cannot have methods with parameters.
Step-by-Step Solution
Solution:
  1. Step 1: Compare method signatures in Parent and Child

    Parent has display() with no parameters; Child has display(int x) with one parameter.
  2. Step 2: Understand @Override annotation rules

    @Override requires exact signature match; here, parameters differ, so it's not overriding.
  3. Final Answer:

    Method display(int x) does not override display() due to different parameters. -> Option C
  4. Quick Check:

    @Override requires exact signature match = C [OK]
Quick Trick: @Override needs exact method signature match to avoid errors [OK]
Common Mistakes:
  • Ignoring parameter difference
  • Thinking @Override can be used on any method
  • Assuming method overloading is overriding

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes