According to the Open-Closed Principle, how should a class be designed?

Get ready for your Object‑Oriented Programming Test. Use flashcards and multiple-choice questions. Each question includes hints and explanations. Prepare for your exam today!

Multiple Choice

According to the Open-Closed Principle, how should a class be designed?

Explanation:
The Open-Closed Principle means a class should be designed to allow new behavior to be added without changing its existing code. This keeps proven functionality safe from new bugs while still letting you extend what the class does. In practice, you achieve this by defining a stable interface or abstract base class and implementing new behavior in new classes that extend or compose with the existing ones. For example, you can swap in different strategies or dependencies at runtime, or create new concrete subclasses that override behavior, all without modifying the original class. That way, you can add features or variations by extending the system, not by touching the original implementation. So the best design is to make a class open for extension but closed for modification. If a class must be edited to add new features, it risks breaking existing behavior; and if it’s closed to extension, you can’t introduce new functionality without changing other parts of the codebase.

The Open-Closed Principle means a class should be designed to allow new behavior to be added without changing its existing code. This keeps proven functionality safe from new bugs while still letting you extend what the class does. In practice, you achieve this by defining a stable interface or abstract base class and implementing new behavior in new classes that extend or compose with the existing ones. For example, you can swap in different strategies or dependencies at runtime, or create new concrete subclasses that override behavior, all without modifying the original class. That way, you can add features or variations by extending the system, not by touching the original implementation.

So the best design is to make a class open for extension but closed for modification. If a class must be edited to add new features, it risks breaking existing behavior; and if it’s closed to extension, you can’t introduce new functionality without changing other parts of the codebase.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy