Bird
Raised Fist0

How would you extend the Vehicle class to support different vehicle types (car, bike, truck) while keeping the design clean?

hard📝 Trade-off Q9 of Q15
LLD - Design — Parking Lot System
How would you extend the Vehicle class to support different vehicle types (car, bike, truck) while keeping the design clean?
ACreate separate unrelated classes for each vehicle type
BAdd a string field 'type' in Vehicle class
CUse inheritance with subclasses Car, Bike, Truck extending Vehicle
DStore vehicle type in Spot class instead
Step-by-Step Solution
Solution:
  1. Step 1: Consider clean design principles

    Using inheritance allows specific behavior per vehicle type.
  2. Step 2: Apply inheritance

    Create subclasses Car, Bike, Truck that extend Vehicle for clarity and extensibility.
  3. Final Answer:

    Use inheritance with subclasses Car, Bike, Truck extending Vehicle -> Option C
  4. Quick Check:

    Inheritance for types = C [OK]
Quick Trick: Use subclasses for different vehicle types [OK]
Common Mistakes:
MISTAKES
  • Using string field leads to messy code
  • Creating unrelated classes breaks hierarchy
  • Storing type in Spot mixes responsibilities

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes