CHAPTER 8 WORKING WITH INTERFACES
Figure 8-6 illustrates the current interface hierarchy.
Figure 8-6. Unlike classes, interfaces can extend multiple interface types
Now, the million dollar question is, if you have a class supporting IShape, how many methods will it
be required to implement? The answer: it depends. If you want to provide a simple implementation of
the Draw() method, you need only provide three members, as shown in the following Rectangle type:
class Rectangle : IShape
{
public int GetNumberOfSides()
{ return 4; }
public void Draw()
{ Console.WriteLine("Drawing..."); }
}
public void Print()
{ Console.WriteLine("Prining..."); }
If you’d rather have specific implementations for each Draw() method (which in this case would
make the most sense), you can resolve the name clash using explicit interface implementation, as shown
in the following Square type:
class Square : IShape
{
// Using explicit implementation to handle member name clash.
void IPrintable.Draw()
{
// Draw to printer ...
}
void IDrawable.Draw()
{
// Draw to screen ...
}
public void Print()
{
// Print ...
}
301