midOOP

What is the Open/Closed Principle (OCP)?

Updated May 17, 2026

Short answer

Software entities should be open for extension but closed for modification.

Deep explanation

OCP encourages adding new behavior via extension instead of modifying existing code, reducing risk of regression in stable systems.

Real-world example

Adding new payment methods without changing existing payment processing logic.

Common mistakes

  • Modifying core logic instead of extending it.

Follow-up questions

  • How do design patterns support OCP?
  • Why is OCP important?

More OOP interview questions

View all →