Object-oriented programming · mid
Small designs, static members and polymorphism in practice
In a design question, name a few classes with clear jobs. Use polymorphism to remove repeated if-else.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- Parking lot: clarify first (vehicle types, spot sizes, pricing, gates). Classes: ParkingLot, Floor, ParkingSpot, Vehicle, Ticket.
- park(vehicle) returns a Ticket; unpark(ticket) returns the amount. A PricingStrategy interface with fee(ticket, exitTime) lets you add pricing rules.
- A map from vehicle type to spot type avoids if-else on the vehicle type in every method.
- Role check on every screen: replace repeated if-else with a Role interface (dashboard(), menuItems(), canEditMarks()) implemented by Student, Teacher, Admin and Parent. A new role is a new class.
- Create the right object in one place, a factory method. That is the only switch left. A plain if-else is fine for two cases that will not grow.
- A static field has one copy shared by all objects. If every student shows the same name, check for a static field.
- Another cause: one Student object created before the loop, changed and added 30 times. A list holds references, not copies.
- Debug by printing each object right after reading it, and by checking identity (System.identityHashCode).
The red pen
Common traps: mistake → fix
Mistake: One giant ParkingLot class is simple
Fix: it does everything and cannot change.
Mistake: Add a 16th else-if
Fix: use polymorphism with a factory.
Mistake: A list stores copies of objects
Fix: it stores references.
Mistake: The SQL is wrong when every name is equal
Fix: check the static field and the loop first.
Easy to hard
Questions interviewers ask, easy to hard
- Design the classes for a small parking lot.
- A role check is repeated on every screen. How do you improve it?
- All students in the list show the same name. What could cause it?
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.