Bird
0
0

Examine the following code snippet and identify the error:

medium📝 Debug Q7 of 15
Java - Inheritance
Examine the following code snippet and identify the error:
class Base {
  void display() {
    System.out.println("Base display");
  }
}
class Derived extends Base {
  void display() {
    super.display();
    System.out.println("Derived display");
  }
  void print() {
    super.print();
  }
}
AThe method <code>print()</code> in Derived calls a non-existent method <code>print()</code> in Base
BThe call to <code>super.display()</code> inside <code>display()</code> is invalid
CDerived class cannot override the display method
DThere is no error in the code
Step-by-Step Solution
Solution:
  1. Step 1: Analyze method calls

    The display() method in Derived correctly calls super.display().
  2. Step 2: Check for method existence

    The print() method in Derived calls super.print(), but Base class does not have a print() method.
  3. Final Answer:

    The method print() in Derived calls a non-existent method print() in Base -> Option A
  4. Quick Check:

    super.method() requires method in parent [OK]
Quick Trick: super calls must match parent methods [OK]
Common Mistakes:
  • Assuming super can call any method
  • Thinking super.display() is invalid
  • Ignoring missing parent methods

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes