Implementing OCP in OOP

Explore how OCP facilitates developing code to welcome new features without requiring modifications to the existing codebase.

In this lesson, we’ll see how OCP (Open-Closed Principle) helps us write code that we can add new features to, without changing the code itself. This does sound like an impossibility at first, but it flows naturally from DIP combined with LSP.

Keeping code open for growth, closed for tweaks

OCP results in code that’s open to extension but closed to modification. We saw this idea at work when we looked at DIP. Let’s now review the code refactoring we did in light of OCP.

Get hands-on with 1200+ tech skills courses.