intermediateSOLID Principles
What is the Open-Closed Principle, and what does 'open for extension, closed for modification' actually mean in practice?
A class should be extendable to support new behavior without changing its existing, already-tested source code. In practice, this usually means depending on an abstraction (an interface) and adding new behavior by writing a NEW class that implements it, rather than adding another if/else branch to an existing class — e.g. adding a new PaymentMethod by creating a new class implementing a PaymentStrategy interface, instead of editing a giant switch statement inside PaymentProcessor.
Ready to master this question?
Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.
Sign in to generate a response