Question
Which SOLID principle emphasizes that software entities
should be open for extension but closed for modification?Solution
The Open/Closed Principle (OCP) is one of the SOLID principles for object-oriented design. 1. Key Idea: Software entities like classes, modules, and functions should be extendable without altering existing code. 2. Implementation: Achieved through techniques like inheritance, polymorphism, or interfaces that allow new functionality to be added without modifying the original codebase. 3. Benefits: Promotes code reusability, reduces the risk of introducing bugs into stable code, and facilitates easier maintenance and scaling. 4. Example: A class for processing payments could use an interface for payment methods. Adding a new payment type would require extending the interface, not modifying the class. This principle ensures that software systems are adaptable to new requirements without jeopardizing existing functionality. Why Other Options Are Incorrect: β’ A) SRP: Focuses on assigning one responsibility to each class/module but does not address extensibility. β’ C) LSP: Ensures derived classes can replace base classes without altering behavior, unrelated to modification. β’ D) ISP: Advocates for creating specific interfaces for clients, reducing unnecessary dependencies. β’ E) DIP: Encourages high-level modules to depend on abstractions rather than concrete implementations.
P purchased a book from registered store and gets 20% discount while Q purchased the same book from a roadside stall and got 24% discount. If Q paid Rs....
The cost price of article A and B is Rs. βXβ and Rs. (X + 750), respectively. Article A is sold at 20% profit while article B is sold at 10% loss. I...
Cost price of an article is Rs. 20,250. It is marked 48% above its cost price and sold after allowing 20% discount. Find the profit (in Rs.) earned
A sold 20% more apples than B. Ratio of number of apples sold by B to number of apples sold by C is 8:9. Find the number of apples sold by A if number o...
- A retailer sold an item for Rs. 1458 after giving two successive discounts of x% each. If the marked price of the item is Rs. 2700, find the value of x.
A machine is sold at 10% profit and its selling price is Rs. 40 more than when sold at a 15% loss. Determine the cost price of the machine.
A vendor marked the price of an article worth Rs 1200 at 50% above cost price and sold it for Rs 1620 after allowing a discount of X%. Find X.
Ajay bought a table Fan and a wall Fan for Rs. __ and Rs. 1500 respectively. He marked the table Fan 20% above the cost price and the Wall fan 25% above...
- Cost price of article βBβ is (250/7)% of selling price of article βAβ. Article βAβ is sold at 40% profit and cost price of article βAβ is R...
A shopkeeper buys an article for Rs 480 and sells it for Rs 564. Find his profit percent.