O'Reilly logo

C# Primer: A Practical Approach by Stanley B. Lippman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required