SOLID & OOP Principles (50 Qs)•Part 1: Core SOLID Principles & Fundamentals (1 - 10)
How do you implement the Open/Closed Principle (OCP) using Polymorphism?
junior Levelsolid-principlesCore SOLID Principles
In Simple Words: You should be able to add new functionality without modifying existing, tested source code:
- Violation: Using a massive
switch(paymentType)statement insideprocessPayment(). Adding ApplePay forces editing existing code! - Refactoring: Define a
PaymentMethodinterface; implementCreditCardPayment,PayPalPayment, andApplePayPaymentas independent classes implementingpay(amount).