Global Reach India UAE USA UK Australia
Open/Closed Principle (OCP) Explained – Stepwise Guide

Open/Closed Principle (OCP) – Complete Guide

What is OCP?

The Open/Closed Principle (OCP) is a core concept in SOLID programming. It states that software entities should be open for extension but closed for modification. This means we can add new functionality without changing existing code, making the system more stable and maintainable.

Why OCP is Important

  • Allows adding new features safely.
  • Prevents unintended side effects when extending functionality.
  • Supports scalable and maintainable architecture.
  • Encourages using abstractions and interfaces.

Stepwise Implementation of OCP

  1. Step 1 – Identify Areas Prone to Change: Find parts of the code that often require modifications.
  2. Step 2 – Use Abstraction: Define interfaces or abstract classes for core behavior.
  3. Step 3 – Extend via New Classes: Implement new functionality using subclasses or new modules.
  4. Step 4 – Avoid Modifying Existing Code: Ensure base code is untouched when adding features.
  5. Step 5 – Test Extensions: Verify that new functionality works without breaking existing behavior.

Example: OCP in Action

Suppose we have a notification system:

  • Base class: Notification
  • Subclass 1: EmailNotification
  • Subclass 2: SMSNotification

Each new notification type can extend the system without modifying existing classes:

interface Notification {
    void send(String message);
}

class EmailNotification implements Notification {
    public void send(String message) {
        // send email
    }
}

class SMSNotification implements Notification {
    public void send(String message) {
        // send SMS
    }
}
      

This approach allows the system to be extended safely, following OCP principles.

Top 10 OCP & SOLID Interview Questions

1. What is Open/Closed Principle (OCP)?
OCP states that classes or modules should be open for extension but closed for modification.
2. Why is OCP important?
It allows adding new functionality without modifying existing code, preventing unintended bugs.
3. Can you give an OCP example?
Notification system: base class Notification, subclasses EmailNotification, SMSNotification. Adding new types doesn't modify existing code.
4. How to implement OCP stepwise?
Identify change-prone areas, use abstractions, extend via new classes, avoid modifying existing code, test extensions.
5. Does OCP apply only to classes?
No, OCP applies to functions, modules, and any structure where extension is needed without modifying existing code.
6. What are OCP benefits?
Improved maintainability, easier feature addition, fewer bugs, cleaner architecture, scalable software.
7. What happens if OCP is violated?
Modifying existing code for new requirements can break current functionality, causing fragile code.
8. How to refactor code violating OCP?
Introduce abstractions or interfaces, and extend functionality via new classes instead of modifying existing ones.
9. Can OCP work with other SOLID principles?
Yes, OCP works with SRP, LSP, ISP, and DIP to maintain clean, modular code.
10. Example of OCP in real life?
Notification system: Notification base class, EmailNotification and SMSNotification subclasses. Adding new notification types doesn't require modifying existing classes.
⚠️ Important Notice: SupportDeskWorld is an independent informational platform. We provide verified, publicly available guides, tutorials, and awareness content. We do not offer direct services, financial advice, legal work, repairs, or government assistance. For official inquiries, please use our Contact Page.
Scroll to Top