Bird
0
0

Identify the error in the following code:

medium📝 Debug Q6 of 15
Java - Inheritance
Identify the error in the following code:
class Parent {
  Parent() {
    System.out.println("Parent constructor");
  }
}
class Child extends Parent {
  Child() {
    super();
    System.out.println("Child constructor");
  }
}
public class Test {
  public static void main(String[] args) {
    Child c = new Child();
  }
}
Asuper() call must be the first statement in constructor
BNo error, code runs fine
CChild constructor cannot call super() explicitly
DParent class constructor is missing
Step-by-Step Solution
Solution:
  1. Step 1: Check constructor and super() usage

    super() is correctly called as the first statement in Child constructor.
  2. Step 2: Verify parent constructor presence

    Parent has a no-arg constructor defined.
  3. Final Answer:

    No error, code runs fine -> Option B
  4. Quick Check:

    super() must be first in constructor = true [OK]
Quick Trick: super() must be first in constructor, else compile error [OK]
Common Mistakes:
  • Placing super() after other statements
  • Assuming parent constructor missing error
  • Thinking explicit super() call is forbidden

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes