Question

    Which SOLID principle emphasizes that a class should

    have only one reason to change?
    A Open/Closed Principle Correct Answer Incorrect Answer
    B Single Responsibility Principle Correct Answer Incorrect Answer
    C Liskov Substitution 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) in SOLID design principles states that a class should have only one reason to change, meaning it should encapsulate only one responsibility. This principle promotes cohesion and ensures that each class handles a single task or functionality. For instance, in an inventory management system, a Product class should handle product-related data only, while a separate Inventory class manages stock levels. Adhering to SRP reduces complexity, makes code easier to understand, and simplifies debugging and testing. By isolating responsibilities, developers can introduce changes without affecting unrelated parts of the system. Why Other Options Are Incorrect :

    1. Open/Closed Principle : Focuses on extending classes without modifying existing code, not on single responsibility.
    2. Liskov Substitution Principle : Ensures that derived classes can be substituted for their base classes without breaking functionality.
    3. Dependency Inversion Principle : Encourages dependency on abstractions rather than concrete implementations, unrelated to single responsibilities.
    4. Interface Segregation Principle : Suggests splitting large interfaces into smaller, specific ones, focusing on interface design, not class responsibilities.

    Practice Next