Question

    Which SOLID principle ensures that a class has only one

    reason to change?
    A Open/Closed Principle Correct Answer Incorrect Answer
    B Liskov Substitution Principle Correct Answer Incorrect Answer
    C Single Responsibility Principle Correct Answer Incorrect Answer
    D Dependency Inversion Principle Correct Answer Incorrect Answer
    E Interface Segregation Principle Correct Answer Incorrect Answer

    Solution

    The Single Responsibility Principle (SRP) states that a class should have only one reason to change, meaning it should have only one responsibility or function. This principle promotes cohesion by ensuring each class handles a specific part of the software functionality. For instance, a Customer class should handle customer data, while an Invoice class manages invoice generation. Adhering to SRP simplifies debugging, enhances maintainability, and prevents interdependent changes from cascading through the codebase. Why Other Options Are Incorrect :

    1. Open/Closed Principle : This emphasizes that classes should be open for extension but closed for modification, unrelated to SRP.
    2. Liskov Substitution Principle : This ensures that derived classes can replace base classes without altering program correctness.
    3. Dependency Inversion Principle : This recommends high-level modules should not depend on low-level modules but rather on abstractions.
    4. Interface Segregation Principle : This advises against forcing a class to implement interfaces it does not use, unrelated to SRP.

    Practice Next