When a new feature is added by extending a class hierarchy following the Open/Closed Principle, what is the typical sequence of events when the system executes a method call on the new subclass instance?
easy🧠🧾 Concept Trace Q12 of Q15
OOP & Design Patterns - Open/Closed Principle - Open for Extension, Closed for Modification
When a new feature is added by extending a class hierarchy following the Open/Closed Principle, what is the typical sequence of events when the system executes a method call on the new subclass instance?
AThe base class method is always executed first, then the subclass method overrides it afterward
BThe system duplicates the base class code inside the subclass to avoid modifying the base
CThe subclass method is invoked directly due to polymorphism, without modifying base class code
DBoth base and subclass methods execute sequentially because the base class is modified to call the subclass
Step-by-Step Solution
Step 1: Understand polymorphic dispatch
Method calls on subclass instances invoke the subclass's overridden method directly.
Step 2: Base class code remains unchanged
The base class is closed for modification; no changes are made to call subclass methods explicitly.
Step 3: Why other options are incorrect
The base class method is always executed first, then the subclass method overrides it afterward incorrectly suggests base method runs first then subclass; Both base and subclass methods execute sequentially because the base class is modified to call the subclass implies base class modification; The system duplicates the base class code inside the subclass to avoid modifying the base suggests code duplication, violating DRY and OCP.
Final Answer:
Option C -> Option C
Quick Check:
Polymorphism enables extension without modifying base code.