Skip to content

Virtual Methods and Overrides

Question

Explain virtual methods. How can you override them?

Short interview answer

A base class marks an overridable member with virtual; a derived class supplies a replacement implementation with override. Calls through the base-class reference dispatch to the override at runtime.

Detailed answer

For example, Shape.Draw() may be virtual and Circle.Draw() may override it. The override must match the inherited member and cannot reduce its accessibility. Use sealed override when a derived implementation must not be overridden again. Prefer interfaces when the capability is not naturally part of one class hierarchy.

Sources