Recall & Review
beginner
What is the virtual method dispatch mechanism in C#?
It is the process where the program decides at runtime which version of a virtual method to call, based on the actual object's type, not the reference type.
Click to reveal answer
beginner
How do you declare a method as virtual in C#?
By using the
virtual keyword before the method return type in the base class.Click to reveal answer
beginner
What keyword is used to override a virtual method in a derived class?
The
override keyword is used to provide a new implementation of a virtual method in a derived class.Click to reveal answer
intermediate
Why is virtual method dispatch important in object-oriented programming?
It enables polymorphism, allowing objects of different classes to be treated uniformly while invoking their specific behaviors at runtime.
Click to reveal answer
intermediate
What happens if a method is not declared virtual but is redefined in a derived class?
The method in the derived class hides the base class method, but calls are resolved at compile time based on the reference type, not the object type.Click to reveal answer
Which keyword in C# allows a method to be overridden in derived classes?
✗ Incorrect
The
virtual keyword marks a method in the base class as overridable.What determines which method implementation is called when using virtual methods?
✗ Incorrect
Virtual method dispatch uses the actual object's type at runtime to decide which method to call.
If a base class method is not virtual, what happens when a derived class defines a method with the same name?
✗ Incorrect
Without
virtual, the derived method hides the base method, and calls depend on reference type.Which keyword must be used in the derived class to provide a new implementation of a virtual method?
✗ Incorrect
The
override keyword tells the compiler this method replaces the base virtual method.What is the main benefit of virtual method dispatch?
✗ Incorrect
Virtual method dispatch enables runtime polymorphism, allowing flexible and dynamic method calls.
Explain how virtual method dispatch works in C# with an example.
Think about a base class with a virtual method and a derived class overriding it.
You got /4 concepts.
Describe the difference between method overriding and method hiding in C#.
Consider what happens when the base method is virtual or not.
You got /4 concepts.