4.5. Inherited Interface Members

Here is a puzzle for you. Given the following three-level hierarchy:

interface IControl
{
    void Paint();
}
class Control: IControl
{
    public void Paint() { ... }
}
class TextBox: Control
{
    new public void Paint() { ... }
}

which instance of Paint() is invoked in the following code sequence?

IControl it  = new TextBox();

// which instance of Paint() is invoked?
it.Paint(); // which instance of Paint() is invoked?

Most people's first response is TextBox.Paint(). After all, it addresses a TextBox object. The IControl interface instance of Paint() is treated as an abstract method. Therefore, the actual method to invoke must be resolved at runtime according to the type of the actual object addressed by it. In this ...

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.