Bird
0
0

Identify the error in the following class with overloaded constructors:

medium📝 Debug Q6 of 15
Java - Constructors

Identify the error in the following class with overloaded constructors:

class Calculator {
Calculator() { System.out.println("Default constructor"); }
Calculator() { System.out.println("Second constructor"); }
}
AMissing return type in constructors.
BDuplicate constructor with same parameter list is not allowed.
CConstructors must have different names.
DConstructors cannot have print statements.
Step-by-Step Solution
Solution:
  1. Step 1: Check constructor signatures

    Both constructors have no parameters, so their signatures are identical.
  2. Step 2: Understand Java rules

    Java does not allow two constructors with the exact same parameter list.
  3. Final Answer:

    Duplicate constructor with same parameter list is not allowed. -> Option B
  4. Quick Check:

    Constructor signatures must differ to overload. [OK]
Quick Trick: Constructors must differ by parameters to overload. [OK]
Common Mistakes:
  • Thinking constructors need return types.
  • Believing constructors can have same parameters if code differs.
  • Assuming constructor names can differ.

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes