Search⌘ K

SOLID: Open Closed Principle

Explore the Open Closed Principle in object-oriented design to learn how to write software that is open for extension but closed for modification. This lesson uses practical examples like shape volume calculations to demonstrate how to structure code for easy scalability, helping you design interview solutions that protect core functionality while adding new features efficiently.

Introduction

In 1988, Bertrand Meyer defined the Open Closed Principle (OCP) in the following way, “A software artifact should be open for extension but closed for modification.” This means that a system should improve easily by adding new code instead of changing the code core. This way, the core code always retains its unique identity, making it reusable.

One might think of OCP as inheritance, but remember that inheritance is only one of the OCP techniques. We use the interface because it is open for extension and closed for modification. Therefore, OCP is also defined as ...