OOP Interview Questions

  1. Encapsulation: Bundling data (attributes) and methods (behavior) that operate on that data within a single unit (class), and restricting direct access to some of an object's components.
  2. Use private/protected attributes with public getters/setters.
  3. Benefit: Hides implementation details, reduces complexity, protects data integrity.

  4. Abstraction: Hiding complex implementation details and showing only the essential features of an object.

  5. Achieved via abstract classes and interfaces.
  6. Benefit: Reduces complexity, isolates changes, improves code readability.

  7. Inheritance: A mechanism where a new class (child) derives properties and behaviors from an existing class (parent).

  8. Supports code reuse and establishes a hierarchical relationship.
  9. Benefit: Eliminates redundant code, models real-world 'is-a' relationships.
  10. Note: Favor composition over inheritance to avoid deep, fragile hierarchies.

  11. Polymorphism: The ability of an object to take on many forms. The same method call can behave differently based on the object's actual type.

  12. Compile-time (Method Overloading): Multiple methods with the same name but different parameters.
  13. Runtime (Method Overriding): A subclass provides a specific implementation of a method defined in its parent.
  14. Benefit: Code flexibility, extensibility, and interface-based design.

Key Points

Encapsulation, Abstraction, Inheritance, Polymorphism, Overloading vs Overriding, Composition over Inheritance

Common Follow-ups

What is the difference between an abstract class and an interface? When would you use each?

SOLID is a set of five design principles for writing maintainable, scalable object-oriented code:

  1. S — Single Responsibility Principle (SRP): A class should have only one reason to change — one job.
  2. O — Open/Closed Principle (OCP): Classes should be open for extension but closed for modification. Use interfaces/abstract classes.
  3. L — Liskov Substitution Principle (LSP): Subtypes must be substitutable for their base types without breaking correctness.
  4. I — Interface Segregation Principle (ISP): Many specific interfaces are better than one general-purpose interface.
  5. D — Dependency Inversion Principle (DIP): Depend on abstractions, not concrete implementations. High-level modules should not depend on low-level modules.

Why SOLID matters? - Reduces coupling between components. - Makes code easier to test (unit testing with mocks). - Allows teams to work on different parts of the codebase independently. - Enables refactoring and extension without breaking existing functionality.

Real-world example: A PaymentProcessor interface allows adding new payment methods (PayPal, Stripe, Crypto) without modifying existing code — following OCP.

Key Points

SOLID acronym, Loose coupling, Testability, Extensibility, Design patterns foundation

Common Follow-ups

Describe a design pattern you've used that aligns with these principles.