Bird
Raised Fist0

What subtle bug might this cause when comparing different vehicle types?

medium🐞 Bug Identification Q7 of Q15
OOP & Design Patterns - Design a Parking Lot - LLD with OOP (Classes, Patterns, Extensibility)
In a parking lot system, a developer implements the Vehicle class hierarchy but overrides the equals() method only in the base Vehicle class. What subtle bug might this cause when comparing different vehicle types?
ADifferent vehicle types may be considered equal if only base class fields are compared
Bequals() method will throw a runtime exception for subclasses
CPolymorphism ensures correct equals() behavior automatically
DComparisons will always return false due to type mismatch
Step-by-Step Solution
Solution:
  1. Step 1: Understand equals() override implications

    Overriding equals() only in base class compares base fields, ignoring subclass-specific fields.
  2. Step 2: Analyze comparison results

    Different vehicle types with same base fields may be incorrectly considered equal.
  3. Step 3: Evaluate other options

    equals() does not throw exceptions (B); polymorphism does not fix equals() logic automatically (C); comparisons won't always be false (A).
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    Base equals() ignores subclass fields causing equality bugs [OK]
Quick Trick: Base equals() ignores subclass fields causing equality bugs [OK]
Common Mistakes:
MISTAKES
  • Assuming polymorphism fixes equals() automatically
  • Expecting exceptions from equals() override
  • Thinking comparisons always fail due to type
Trap Explanation:
PITFALL
  • Candidates overlook that equals() must be carefully overridden in subclasses to avoid incorrect equality.
Interviewer Note:
CONTEXT
  • Tests subtle OOP pitfalls in method overriding and equality.
Master "Design a Parking Lot - LLD with OOP (Classes, Patterns, Extensibility)" in OOP & Design Patterns

2 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More OOP & Design Patterns Quizzes