Bird
Raised Fist0

A developer believes that simply adding new methods to an existing class without changing existing methods satisfies the Open/Closed Principle. What is the flaw in this reasoning?

medium🐞 Bug Identification Q7 of Q15
OOP & Design Patterns - Open/Closed Principle - Open for Extension, Closed for Modification
A developer believes that simply adding new methods to an existing class without changing existing methods satisfies the Open/Closed Principle. What is the flaw in this reasoning?
AAdding methods is extension only if existing methods are not modified, so this satisfies OCP.
BOCP only applies to adding new classes, not methods.
CAdding methods modifies the class interface and implementation, violating OCP.
DAdding methods is allowed if they are private.
Step-by-Step Solution
Solution:
  1. Step 1: Clarify OCP scope

    OCP requires that existing code entities are not modified; adding methods changes the class interface and implementation.
  2. Step 2: Analyze options

    Adding methods is extension only if existing methods are not modified, so this satisfies OCP incorrectly assumes adding methods is extension without modification. Adding methods modifies the class interface and implementation, violating OCP correctly states adding methods modifies the class, violating OCP. OCP only applies to adding new classes, not methods is false; OCP applies to all code entities. Adding methods is allowed if they are private is false; private methods still modify the class.
  3. Final Answer:

    Option C -> Option C
  4. Quick Check:

    Adding methods changes class, violating OCP [OK]
Quick Trick: Adding methods modifies class, violating OCP [OK]
Common Mistakes:
MISTAKES
  • Confusing adding methods with extension
  • Thinking private methods don't modify class
Trap Explanation:
PITFALL
  • Candidates think adding methods is harmless extension, ignoring that it modifies existing code structure.
Interviewer Note:
CONTEXT
  • Identifies subtle misunderstanding of what constitutes modification under 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