Bird
0
0

Find the error in this Java code related to inheritance:

medium📝 Debug Q6 of 15
Java - Inheritance
Find the error in this Java code related to inheritance:
class Parent {
  void display() { System.out.println("Parent"); }
}
class Child extends Parent {
  void display() { System.out.println("Child"); }
}
public class Test {
  public static void main(String[] args) {
    Child c = new Parent();
    c.display();
  }
}
AMethod display() is missing in Child class
BNo error, code runs fine
CClass Child cannot extend Parent
DCannot assign Parent object to Child reference
Step-by-Step Solution
Solution:
  1. Step 1: Check object assignment compatibility

    A child class reference cannot point to a parent class object because parent may lack child features.
  2. Step 2: Identify the error line

    Line Child c = new Parent(); causes compile error due to incompatible types.
  3. Final Answer:

    Cannot assign Parent object to Child reference -> Option D
  4. Quick Check:

    Child reference cannot hold Parent object [OK]
Quick Trick: Child reference cannot point to Parent object [OK]
Common Mistakes:
  • Thinking inheritance allows any assignment
  • Ignoring compile-time type checks
  • Assuming method presence fixes assignment error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes