Open–closed principle

In object-oriented programming, a class which follows the open/closed principle is a class which is both open and closed, where open means it has the ability to be extended and closed means it cannot be modified other than by extension.

Those who advocate the open/closed principle reason that once a class has been approved for use having gone through code reviews, unit tests, and other qualifying procedures, typically one does not want to change the class very much, just extend it.

The name Open/Closed Principle has been used in two ways. Both ways use inheritance to resolve the apparent dilemma, but the goals, techniques, and results are different.

Meyer's Open/Closed Principle
Dr. Bertrand Meyer is generally credited as having originated the term Open/Closed Principle, which appeared in his 1988 book Object Oriented Software Construction. The idea was that once completed, the implementation of a class could only be modified to correct errors; new or changed features would require that a different class be created. That class could reuse coding from the original class through inheritance. The derived subclass might or might not have the same interface as the original class.

Meyer's definition advocates implementation inheritance. Implementation can be reused through inheritance but interface specifications need not be. The existing implementation is closed to modifications, and new implementations need not implement the existing interface.

Polymorphic Open/Closed Principle
During the 1990s, the Open/Closed Principle became popularly redefined to refer to the use of abstracted interfaces, where the implementations can be changed and multiple implementations could be created and polymorphically substituted for each other.

In contrast to Meyer's usage, this definition advocates virtual inheritance. Interface specifications can be reused through inheritance but implementation need not be. The existing interface is closed to modifications and new implementations must, at a minimum, implement that interface.

Robert C. Martin's 1996 article "The Open-Closed Principle" was one of the seminal writings to take this approach. In 2001 Craig Larman would write that the Open/Closed Principle "is what David Parnas really meant by information hiding".