Skip to main content
InterviewHubPRO
Section Overview
Q3

Question 3 of 50

Part 1: Core SOLID Principles & Fundamentals (1 - 10)

6%
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 inside processPayment(). Adding ApplePay forces editing existing code!
  • Refactoring: Define a PaymentMethod interface; implement CreditCardPayment, PayPalPayment, and ApplePayPayment as independent classes implementing pay(amount).