What happens when a virtual method is called on a base class reference that points to a derived class object?
easy🧠 Conceptual Q1 of 15
C Sharp (C#) - Polymorphism and Abstract Classes
What happens when a virtual method is called on a base class reference that points to a derived class object?
AThe base class's method is always executed.
BThe derived class's overridden method is executed.
CA compile-time error occurs.
DThe program crashes at runtime.
Step-by-Step Solution
Solution:
Step 1: Understand virtual method behavior
Virtual methods allow derived classes to override base class methods and provide their own implementation.
Step 2: Analyze method call on base reference
When calling a virtual method on a base class reference that points to a derived object, the derived class's overridden method is invoked due to dynamic dispatch.
Final Answer:
The derived class's overridden method is executed. -> Option B
Quick Check:
Virtual method dispatch = Derived method called [OK]
Quick Trick:Virtual methods call derived overrides via base references [OK]
Common Mistakes:
MISTAKES
Assuming base method always runs
Confusing virtual with non-virtual methods
Expecting compile-time error
Master "Polymorphism and Abstract Classes" in C Sharp (C#)
9 interactive learning modes - each teaches the same concept differently