Practice
1. In designing the Vehicle hierarchy for the booking system's parking module, should
Vehicle be an abstract class or an interface, and why?easy
Solution
Step 1: Identify if common state exists in Vehicle
Vehicle has shared fields like license plate, size, or type, which require storage and partial implementation.Step 2: Abstract class fits because it allows shared state and behavior, while interfaces cannot hold instance state.
Using an abstract class enforces common code reuse and prevents direct instantiation.Final Answer:
Option B -> Option BQuick Check:
Common state + shared behavior -> abstract class [check]
Hint: State needs abstract class; behavior-only needs interface [check]
Common Mistakes:
2. Given a
Vehicle abstraction that requires both state (e.g., speed) and behavior (e.g., accelerate()), should Vehicle be an abstract class or an interface in Java, and why?easy
Solution
Step 1: Analyze state and behavior requirements
Vehicleneeds to hold state like speed and provide behavior like accelerate(), which requires shared fields and possibly implemented methods.Step 2: Choose between abstract class and interface
Abstract classes can hold instance variables and provide method implementations, while interfaces (pre-Java 8) cannot hold state and only declare methods. Even with default methods, interfaces cannot hold mutable state.Final Answer:
Option D -> Option DQuick Check:
State + behavior -> abstract class fits better than interface [check]
Hint: State needs fields -> use abstract class, not interface [check]
Common Mistakes:
3. Given a
Vehicle abstraction that requires both state (like speed) and behavior (like accelerate), should Vehicle be an abstract class or an interface in Java, and why?easy
Solution
Step 1: Analyze state and behavior requirements
Vehicle needs to hold state like speed and provide behavior like accelerate with shared code, which requires fields and method implementations.Step 2: Compare abstract class vs interface capabilities
Abstract classes can have instance variables and concrete methods; interfaces cannot hold instance state (only constants) and default methods cannot maintain state.Final Answer:
Option C -> Option CQuick Check:
State + behavior with shared code -> abstract class [check]
Hint: State needs fields? Use abstract class, not interface [check]
Common Mistakes:
4. You need to add support for
ElectricCar and ChargingSpot in your existing parking system without modifying existing classes. What is the minimum change to support this extensibility in Java?hard
Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and new subclass ElectricCar extends Vehicle allows extension without changing existing class code.Step 2: Update SpotCompatibility map externally
Updating compatibility map to include new types supports new logic without modifying existing classes, preserving OCP.Final Answer:
Option C -> Option CQuick Check:
New enums + subclass + external map update -> no existing class changes [check]
Hint: New enums + subclass + map update -> no existing class change [check]
Common Mistakes:
5. To add
ElectricCar needing ChargingSpot compatibility, what is the minimum change to the current design without modifying existing classes?hard
Solution
Step 1: Identify extensibility without modifying existing classes
Adding new vehicle and spot types via enums and creating a new subclassElectricCarextends functionality without changing existing code.Step 2: Update compatibility map externally
UpdatingSpotCompatibility.COMPATIBILITY_MAPto include new mappings allows the system to recognize charging spots without modifying core logic.Final Answer:
Option C -> Option CQuick Check:
New enums + subclass + compatibility map update -> minimal change, OCP preserved [check]
Hint: New enums + subclass + map update -> extend without modify [check]
Common Mistakes:
