Bird
0
0

Identify the error in this code snippet:

medium📝 Debug Q6 of 15
Java - Constructors
Identify the error in this code snippet:
class Parent {
  Parent(int x) {}
}
class Child extends Parent {
  Child() {}
}
ANo error, code compiles fine
BParent constructor must be no-argument
CChild class cannot extend Parent with parameterized constructor
DChild constructor must call Parent constructor explicitly
Step-by-Step Solution
Solution:
  1. Step 1: Check Parent constructor

    Parent has only a parameterized constructor, no default constructor.
  2. Step 2: Check Child constructor

    Child's constructor does not call super(int), so compiler error occurs because no default super() exists.
  3. Final Answer:

    Child constructor must call Parent constructor explicitly -> Option D
  4. Quick Check:

    Missing explicit super call = error [OK]
Quick Trick: If parent has no default constructor, child must call super(params) [OK]
Common Mistakes:
  • Assuming default super() exists
  • Thinking parameterized parent constructor is optional
  • Ignoring compiler error on missing super call

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes