Bird
Raised Fist0

What is the impact on the system's adherence to the Open/Closed Principle?

easy🧠🧾 Concept Trace Q3 of Q15
OOP & Design Patterns - Open/Closed Principle - Open for Extension, Closed for Modification
A developer adds a new payment method by creating a new class implementing the Payment interface without changing existing classes. What is the impact on the system's adherence to the Open/Closed Principle?
AThe system violates OCP because new classes should not be added.
BThe system adheres to OCP only if the new class modifies existing classes.
CThe system violates OCP because interfaces should not be extended.
DThe system adheres to OCP because existing code remains unchanged and new behavior is added.
Step-by-Step Solution
Solution:
  1. Step 1: Recall OCP definition

    OCP allows extension by adding new code without modifying existing code.
  2. Step 2: Analyze scenario

    Adding a new class implementing an interface extends functionality without changing existing classes, thus adhering to OCP.
  3. Step 3: Evaluate options

    The system violates OCP because new classes should not be added is false; adding classes is allowed. The system violates OCP because interfaces should not be extended is false; interfaces can be extended. The system adheres to OCP only if the new class modifies existing classes is false; modifying existing classes breaks OCP.
  4. Final Answer:

    Option D -> Option D
  5. Quick Check:

    New class added, no existing code changed [OK]
Quick Trick: Adding new classes implementing interfaces extends without modification [OK]
Common Mistakes:
MISTAKES
  • Believing adding classes violates OCP
  • Confusing interface extension with class modification
Trap Explanation:
PITFALL
  • Candidates confuse adding new classes with modifying existing code, misunderstanding OCP's allowance for extension.
Interviewer Note:
CONTEXT
  • Tests conceptual understanding of extension vs modification in OCP.
Master "Open/Closed Principle - Open for Extension, Closed for Modification" in OOP & Design Patterns

2 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More OOP & Design Patterns Quizzes