Bird
0
0

Find the error in this code snippet:

medium📝 Debug Q6 of 15
Java - Inheritance

Find the error in this code snippet:

class Parent {
    void display() {
        System.out.println("Parent display");
    }
}
class Child extends Parent {
    void display(int x) {
        System.out.println("Child display " + x);
    }
}
public class Test {
    public static void main(String[] args) {
        Child c = new Child();
        c.display();
    }
}
ACompilation error: display() method not found in Child
BNo error, code runs and prints 'Parent display'
CRuntime error due to method overloading
DCompilation error: display(int) must override display()
Step-by-Step Solution
Solution:
  1. Step 1: Understand method overloading vs overriding

    Child class has display(int), which overloads but does not override display().
  2. Step 2: Check method call resolution

    Calling display() with no arguments calls Parent's display() method inherited by Child.
  3. Final Answer:

    No error, code runs and prints 'Parent display' -> Option B
  4. Quick Check:

    Overloading adds methods, does not remove parent methods [OK]
Quick Trick: Overloading adds methods; overriding replaces methods [OK]
Common Mistakes:
  • Thinking overloading removes parent methods
  • Expecting compilation error for missing override
  • Confusing overloading with overriding

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes