3.10. Overriding the Inherited Virtual Interface

A derived class can either inherit or override the virtual interface of its base class. To inherit a member, the class need do nothing. Inheritance is the default behavior. To override an inherited abstract or virtual member, however, we must label the definition with the override keyword. Otherwise the compiler treats the instance as an independent definition reusing the name of the inherited member. If you do this—accidentally or not—you'll receive a warning from the compiler. If the warning is spurious, you can turn it off by specifying the new keyword; we see how to do this in Section 3.12.

Both the base-class and derived-class instances of the virtual function must have the same access level, ...

Get C# Primer: A Practical Approach now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.