ArticleslgStudy

computer science

Open–closed principle

Open–closed principle is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Open–closed principle rather than just read about it. In short: In object-oriented programming, the open–closed principle (OCP) states "software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification"; that is, such an entity can allow its behaviour to be extended without modifying its source code. The name open–closed principle has been used in two ways.

Open–closed principle — main illustration
Open–closed principle — illustration

Key takeaways

  • Open–closed principle belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Open–closed principle to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Open–closed principle from memory before moving on to harder problems.

Reference excerpt

In object-oriented programming, the open–closed principle (OCP) states "software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification"; that is, such an entity can allow its behaviour to be extended without modifying its source code. The name open–closed principle has been used in two ways. Both ways use generalizations (for instance, inheritance or delegate functions) to resolve the apparent dilemma, but the goals, techniques, and results are different. The open–closed principle is one of the five SOLID principles of object-oriented design.

Meyer's open–closed principle Bertrand Meyer is generally credited for having originated the term open–closed principle, which appeared in his 1988 book Object-Oriented Software Construction.

A module will be said to be open if it is still available for extension. For example, it should be possible to add fields to the data structures it contains, or new elements to the set of functions it performs. A module will be said to be closed if [it] is available for use by other modules. This assumes that the module has been given a well-defined, stable description (the interface in the sense of information hiding).

At the time Meyer was writing, adding fields or functions to a library inevitably required changes to any programs depending on that library. Meyer's proposed solution to this problem relied on the notion of object-oriented inheritance (specifically implementation inheritance):

A class is closed, since it may be compiled, stored in a library, baselined, and used by client classes. But it is also open, since any new class may use it as parent, adding new features. When a descendant class is defined, there is no need to change the original or to disturb its clients.

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 inheritance from abstract base classes. 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 related the open–closed principle to the pattern by Alistair Cockburn called Protected Variations, and to the David Parnas discussion of information hiding.

See also SOLID – the "O" in "SOLID" represents the open–closed principle Robustness principle

References

External links The Principles of OOD The Open/Closed Principle: Concerns about Change in Software Design The Open-Closed Principle -- and What Hides Behind It

Illustrations

Open–closed principle: The open-closed principle was introduced by Bertrand Meyer.
The open-closed principle was introduced by Bertrand Meyer.

Worked examples

Example 1 — a first encounter with Open–closed principle

Start with the simplest possible case. Write down what Open–closed principle claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Open–closed principle before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Open–closed principle ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Open–closed principle

In research
Open–closed principle appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Open–closed principle in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Open–closed principle is common in secondary-school and first-year university syllabi. It links to neighbouring topics Object-oriented programming, Programming principles, Software design, so understanding it makes those chapters shorter.
In everyday life
Look for Open–closed principle outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Open–closed principle” →

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Open–closed principle in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Open–closed principle means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Open–closed principle out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Open–closed principle in simple terms?

In object-oriented programming, the open–closed principle (OCP) states "software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification"; that is, such an entity can allow its behaviour to be extended without modifying its source code. The name open–c…

Why does Open–closed principle matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Open–closed principle?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Open–closed principle.

Tags

  • Object-oriented programming
  • Programming principles
  • Software design
  • Type theory

Keep exploring