Question
Which SOLID principle emphasizes that a class should
have only one reason to change?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 :
- Open/Closed Principle : Focuses on extending classes without modifying existing code, not on single responsibility.
- Liskov Substitution Principle : Ensures that derived classes can be substituted for their base classes without breaking functionality.
- Dependency Inversion Principle : Encourages dependency on abstractions rather than concrete implementations, unrelated to single responsibilities.
- Interface Segregation Principle : Suggests splitting large interfaces into smaller, specific ones, focusing on interface design, not class responsibilities.
- The ratio between Riya's income and expenditure is 5:3. If both income and expenditure rise by 60% and 40% respectively, find the percentage increase in he...
- The number of cats and dogs in a shelter are in the ratio 6:5. If the number of cats decreased by 24 and number of dogs increased by 6, their numbers would...
Shahrukh, Salman and Aamir invested a sum of Rs. 250000 in a business. Salman invested Rs. 30000 more than Shahrukh and Aamir invested Rs. 40000 more th...
- The ratio of the monthly income of Pooja and Meena is 5:3. Pooja spends Rs. 7500, while Meena spends Rs. 4500. Their combined savings sum up to Rs. 6000. F...
In what proportion must a grocer mix sugar at Rs.12 a kg and Rs.7 a kg so as to make a mixture worth Rs.8 a kg ?
In a class, 3/5 of the students are girls and rest are boys. If 2/9 of the girls and 1/4 of the boys are absent. What part of the total number of studen...
In a box, there are some coins and rings which are either silver or gold. If 40% of the objects are rings and 50% of the coins are silver, then find out...
A company produces three types of products: X, Y, and Z. Initially, the production ratio of X to Y to Z is 5:7:8. The total production of these three pr...
A factory produces three types of products: A, B, and C. Initially, the production ratio of A to B to C is 3:4:5. The total number of products produced ...