OOP and SOLID
22 interview questions and answers in the OOP and SOLID section.
Questions in this section
- What is Single Responsibility Principle and How to Apply It
- Give an Example of Single Responsibility Principle Violation
- What is Open-Closed Principle?
- How to Refactor Code that Violates Open-Closed Principle?
- What is Liskov Substitution Principle?
- Give an Example of Liskov Substitution Principle Violation
- What is Interface Segregation Principle?
- What is Dependency Inversion Principle?
- Why Do We Need SOLID Principles at All?
- What is Composition and Inheritance
- When Is It Better to Use Composition Instead of Inheritance
- What is Delegation in OOP
- How is Single Responsibility Related to Cohesion
- What Happens if a Class Has Multiple Reasons to Change
- How Does SOLID Help in Code Testing?
- How is Dependency Inversion Related to Dependency Injection?
- What is Law of Demeter (Principle of Least Knowledge)?
- How to Refactor God Object?
- How Do SOLID Principles Help When Extending Functionality?
- Can You Follow All SOLID Principles at Once?
- How to Determine if a Class Has Single Responsibility?
- What Anti-patterns Contradict SOLID Principles?
Study navigator
22 questions for preparing for a Middle Java Developer interview.
π All Questions
πΊοΈ Topic Dependency Map
ββββββββββββββββββββββββββββββββββββββββββββ
β SOLID BASICS (1-9) β
β 1-2. SRP (what + violation example) β
β 3-4. OCP (what + refactoring) β
β 5-6. LSP (what + violation example) β
β 7. ISP β
β 8. DIP β
β 9. Why SOLID β
ββββββββββββββββββββ¬ββββββββββββββββββββββββ
β
ββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββ
βΌ βΌ βΌ
βββββββββββββββββ βββββββββββββββββ ββββββββββββββββββββββ
β COMPOSITION β β DEEPER β β PRACTICE β
β AND INHERIT. β β SRP/DIP β β AND ANTI-PATTERNS β
β (10-12) β β (13-16) β β (17-22) β
β 10. Compositionβ β 13. SRP + β β 17. Law of Demeter β
β 11. When whichβ β cohesion β β 18. God Object β
β 12. Delegat. β β 14. Multiple β β 19. Extending β
β β β reasons β β 20. All SOLID at onceβ
β β β 15. Testing β β 21. How to determineβ
β β β 16. DIP vs DI β β 22. Anti-patterns β
βββββββββββββββββ βββββββββββββββββ ββββββββββββββββββββββ
π― Recommended Study Order
π’ Junior Level (weeks 1-2)
| Step | Topic | Files | Goal |
|---|---|---|---|
| 1 | SOLID overview | Q9 | Why we need principles |
| 2 | SRP | Q1, Q2 | Single responsibility, violation examples |
| 3 | OCP | Q3 | Open for extension, closed for modification |
| 4 | Composition vs inheritance | Q10, Q11 | Why composition is preferred |
| 5 | Delegation | Q12 | How delegation works |
π‘ Middle Level (weeks 3-4)
| Step | Topic | Files | Goal |
|---|---|---|---|
| 1 | LSP | Q5, Q6 | Substitutability of subclasses, violation examples |
| 2 | ISP | Q7 | Donβt force implementing unnecessary methods |
| 3 | DIP | Q8, Q16 | Dependency on abstractions, relation to DI |
| 4 | SRP + Cohesion | Q13 | How SRP affects cohesion |
| 5 | Consequences of violation | Q14, Q15 | What happens with SRP violation, how SOLID helps testing |
| 6 | Law of Demeter | Q17 | Principle of least knowledge |
π΄ Senior Level (weeks 5-6)
| Step | Topic | Files | Goal |
|---|---|---|---|
| 1 | OCP Refactoring | Q4 (Senior) | Strangler Fig, Strategy, Visitor, SPI |
| 2 | God Object Refactoring | Q18 | Progressive refactoring, Bounded Contexts |
| 3 | SOLID Trade-offs | Q20 | When SOLID conflicts with itself |
| 4 | Determining SRP | Q21 | Stakeholder analysis, cross-cutting concerns |
| 5 | Anti-patterns | Q22 | STUPID mnemonic, Speculative Generality |
| 6 | Extending Functionality | Q19 | YAGNI, Strangler Fig |
π Key Connections Between Topics
Topic: SOLID Principles
Q9 (Why SOLID) β Q1 (SRP) β Q3 (OCP) β Q5 (LSP) β Q7 (ISP) β Q8 (DIP)
β
Q13 (SRP+Cohesion) β Q14 (Multiple reasons) β Q15 (Testing)
Key connections:
- Q9 β Q22: SOLID protects against STUPID anti-patterns
- Q1 β Q13: SRP β high cohesion β lower LCOM
- Q3 β Q4: OCP concept β refactoring to OCP
- Q5 β Q6: LSP theory β concrete violation examples (Date, SavingsAccount)
Topic: Composition and Inheritance
Q10 (Composition vs inheritance) β Q11 (When which) β Q12 (Delegation)
Key connections:
- Q10 β Q11: Concept β practical selection rules
- Q11 β Q8: Composition β DIP (depend on interface, not implementation)
- Q12 β Q10: Delegation = mechanism of composition
Topic: Practice and Anti-patterns
Q17 (Law of Demeter) β Q18 (God Object) β Q22 (Anti-patterns)
β β
Q19 (Extending) β Q20 (All SOLID) β Q21 (How to determine SRP)
Key connections:
- Q17 β Q18: Train Wreck β God Object β refactoring
- Q18 β Q22: God Object β one of the STUPID anti-patterns
- Q19 β Q4: Extending functionality β OCP refactoring
- Q20 β Q9: SOLID trade-offs β why we need them
π Cheat Sheet: What to Know for Each Level
π’ Junior
- SRP: one reason to change = one stakeholder
- OCP: add new classes, donβt modify old ones
- Composition > inheritance: has-a is better than is-a
- DIP: depend on interfaces, not implementations
- ISP: donβt force implementing unnecessary methods
π‘ Middle
- LSP: subclass must not strengthen preconditions or weaken postconditions
- Cohesion: SRP β high cohesion, LCOM metric
- Law of Demeter: donβt talk to strangers (a.b.c() = Train Wreck)
- DI vs DIP: DI is a mechanism, DIP is an architectural rule
- Delegation: black box, only public interface
π΄ Senior
- OCP refactoring: Strangler Fig, Strategy, Visitor, SPI
- God Object refactoring: Progressive refactoring, Bounded Contexts, Saga
- SOLID trade-offs: when principles conflict (SRP vs DIP, OCP vs YAGNI)
- Megamorphic calls: 3+ interface implementations β performance degradation
- Sealed interface (Java 17+): pattern matching, compiler guarantees exhaustiveness
- STUPID: Singleton, Tight Coupling, Premature Optimization, Indescriptive Naming, Duplication
π File Format
Each file contains:
- π’ Junior Level β basic understanding, simple analogies, examples
- π‘ Middle Level β internals, common mistakes, practical examples
- π΄ Senior Level β deep dive, edge cases, production experience, monitoring
- π― Interview Cheat Sheet β key points, common questions, red flags, related topics