Which SOLID principle states software entities should be open for extension but closed for modification?

Prepare for the FAST Enterprises IC Interview. Enhance your skills with flashcards and multiple-choice questions. Each question provides hints and detailed explanations. Excel in your interview!

Multiple Choice

Which SOLID principle states software entities should be open for extension but closed for modification?

Explanation:
The main concept being tested is designing software so you can add new behavior without changing existing code. The Open/Closed Principle says software entities should be open for extension but closed for modification. In practice, this means structuring code so you can introduce new functionality by adding new classes or modules that implement existing abstractions, rather than altering the existing implementations. Using interfaces or abstract base classes lets client code work with any new implementation that conforms to the same contract, keeping old code stable while you extend capabilities. For example, if you have a Shape interface, you can add new shapes like Triangle by creating a new class that implements Shape, and the code processing shapes continues to work without modification. This approach reduces risk and makes the system easier to maintain as features grow. The other principles aren’t describing extension through new behavior without touching existing code: Single Responsibility focuses on keeping a class narrowly focused on one reason to change, Dependency Inversion emphasizes depending on abstractions rather than concretions, and Interface Segregation promotes smaller, client-specific interfaces.

The main concept being tested is designing software so you can add new behavior without changing existing code. The Open/Closed Principle says software entities should be open for extension but closed for modification. In practice, this means structuring code so you can introduce new functionality by adding new classes or modules that implement existing abstractions, rather than altering the existing implementations. Using interfaces or abstract base classes lets client code work with any new implementation that conforms to the same contract, keeping old code stable while you extend capabilities. For example, if you have a Shape interface, you can add new shapes like Triangle by creating a new class that implements Shape, and the code processing shapes continues to work without modification. This approach reduces risk and makes the system easier to maintain as features grow. The other principles aren’t describing extension through new behavior without touching existing code: Single Responsibility focuses on keeping a class narrowly focused on one reason to change, Dependency Inversion emphasizes depending on abstractions rather than concretions, and Interface Segregation promotes smaller, client-specific interfaces.

Subscribe

Get the latest from Passetra

You can unsubscribe at any time. Read our privacy policy