illustrates interface member hiding
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example8_8.cs illustrates interface member hiding
*/
using System;
// define the IDrivable interface
public interface IDrivable
{
void TurnLeft();
}
// define the ISteerable interface (derived from IDrivable)
public interface ISteerable : IDrivable
{
new void TurnLeft(); // hides TurnLeft() in IDrivable
}
// Car class implements the IMovable interface
class Car : ISteerable
{
// explicitly implement the TurnLeft() method of the ISteerable interface
void ISteerable.TurnLeft()
{
Console.WriteLine("ISteerable implementation of TurnLeft()");
}
// implement the TurnLeft() method of the IDrivable interface
public void TurnLeft()
{
Console.WriteLine("IDrivable implementation of TurnLeft()");
}
}
public class Example8_8
{
public static void Main()
{
// create a Car object
Car myCar = new Car();
// call myCar.TurnLeft()
Console.WriteLine("Calling myCar.TurnLeft()");
myCar.TurnLeft();
// cast myCar to ISteerable
ISteerable mySteerable = myCar as ISteerable;
Console.WriteLine("Calling mySteerable.TurnLeft()");
mySteerable.TurnLeft();
// cast myCar to IDrivable
IDrivable myDrivable = myCar as IDrivable;
Console.WriteLine("Calling myDrivable.TurnLeft()");
myDrivable.TurnLeft();
}
}
Related examples in the same category