December 2001
Intermediate to advanced
416 pages
8h 16m
English
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 ...
Read now
Unlock full access