SOLID
| Principle | Interview Meaning |
|---|---|
| Single Responsibility | One class should have one reason to change |
| Open/Closed | Open for extension, closed for modification |
| Liskov Substitution | Child classes should be usable where parent classes are expected |
| Interface Segregation | Do not force clients to depend on methods they do not use |
| Dependency Inversion | Depend on abstractions, not concrete low-level classes |
Coupling and Cohesion
Cohesion is about how closely related responsibilities inside one module are. High cohesion is good.
Coupling is about how much one module depends on another. Low coupling is good.
Interview Framing
A good answer connects SOLID to maintainability: easier testing, lower blast radius, clearer responsibilities, and safer future changes.
Interview Scenario Practice
Scenario 1: Invoice Class Prints, Saves, and Calculates
Scenario: One Invoice class calculates totals, saves to DB, and prints PDFs.
Strong answer: This violates Single Responsibility. Split calculation, persistence, and printing into focused collaborators.
Why it works: Each class then has fewer reasons to change and becomes easier to test.
Common mistake: Thinking SRP means one method per class. It means one reason to change.
Scenario 2: Adding a New Payment Type
Scenario: Every new payment method requires editing a large if-else chain.
Strong answer: Apply Open/Closed and Dependency Inversion by depending on a PaymentProcessor interface and adding new implementations.
Why it works: New behavior is added through extension instead of repeated modification of existing code.
Common mistake: Hiding the if-else in another class without changing the design pressure.
Scenario 3: Fat Interface
Scenario: A printer class is forced to implement scan() and fax() methods it cannot support.
Strong answer: This violates Interface Segregation. Split the interface into smaller capabilities like Printable, Scannable, and Faxable.
Why it works: Implementations only depend on methods they actually use.
Common mistake: Throwing UnsupportedOperationException from many unused interface methods.