Bird
Raised Fist0

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
  1. Step 1: Understand polymorphic dispatch

    Method calls on subclass instances invoke the subclass's overridden method directly.
  2. Step 2: Base class code remains unchanged

    The base class is closed for modification; no changes are made to call subclass methods explicitly.
  3. 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.
  4. Final Answer:

    Option C -> Option C
  5. Quick Check:

    Polymorphism enables extension without modifying base code.
Quick Trick: Polymorphism calls subclass methods directly [OK]
Common Mistakes:
MISTAKES
  • Assuming base method always runs first
  • Believing base class must be modified to support extension
  • Thinking code duplication is a valid OCP strategy
Trap Explanation:
PITFALL
  • Options A and C confuse method overriding with method chaining or base modification; B distracts with duplication which is not OCP-compliant.
Interviewer Note:
CONTEXT
  • Checks understanding of dynamic dispatch and how OCP leverages polymorphism at runtime.
Master "Open/Closed Principle - Open for Extension, Closed for Modification" in OOP & Design Patterns

2 interactive learning modes - each teaches the same concept differently

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More OOP & Design Patterns Quizzes