Practice
NotificationChannel be an abstract class or an interface? Consider that each channel has state like connection status and common methods like connect() and send(). Why?Solution
Step 1: Analyze state and behavior requirements
NotificationChannel needs to hold connection status (state) and provide common method implementations.Step 2: Choose between interface and abstract class
Interfaces cannot hold instance state; abstract classes can hold state and provide default behavior, making abstract class the correct choice.Final Answer:
Option A -> Option AQuick Check:
Abstract class supports shared state and default methods [check]
Order entity be designed as an abstract class or an interface? Consider that orders have common state fields like orderId and timestamp, but different types of orders have distinct execution logic.Solution
Step 1: Analyze state vs behavior requirements
Orders share common state fields likeorderIdandtimestamp, which require storage and possibly shared methods.Step 2: Why abstract class fits better
Abstract classes can hold state and provide default method implementations, reducing code duplication. Interfaces cannot hold instance state.Final Answer:
Option A -> Option AQuick Check:
Shared state + default behavior -> abstract class [check]
FeeCalculator class. To add a WeekendFeeStrategy without modifying existing classes, which SOLID principle applies and what is the minimal correct change?Solution
Step 1: Identify applicable SOLID principles
Open-Closed Principle (OCP) requires FeeCalculator not be modified for new fee types. Dependency Inversion Principle (DIP) requires FeeCalculator depend on abstractions.Step 2: Apply correct design
Create WeekendFeeStrategy implementing FeeStrategy interface. Inject FeeStrategy into FeeCalculator. This adds new behavior without modifying existing code.Final Answer:
Option C -> Option CQuick Check:
New strategy class + DI -> OCP and DIP respected [check]
ElectricCar playback devices that require ChargingSpot management. What is the minimum change to the current design to support this without modifying existing classes?Solution
Step 1: Identify extensibility without modifying existing classes
Adding new enum values and subclassing allows extension without changing existing class code.Step 2: Update compatibility externally
Updating SpotCompatibility map outside existing classes respects OCP and avoids class explosion or god-class logic.Final Answer:
Option A -> Option AQuick Check:
Extend enums + subclass + external map update -> minimal change [check]
ElectricCar and ChargingSpot without modifying existing Vehicle or Spot classes. What is the minimum change to support this extensibility in Java?Solution
Step 1: Identify extensibility without modifying existing classes
Enums can be extended with new constants; subclassingElectricCaradds new behavior without changingVehicle.Step 2: Update compatibility externally
UpdatingSpotCompatibility.COMPATIBILITY_MAPexternally respects OCP and avoids class explosion or god classes.Final Answer:
Option B -> Option BQuick Check:
Extend enums + subclass + external map update -> minimal change [check]
