CHAPTER 6 UNDERSTANDING INHERITANCE AND POLYMORPHISM
{
}
Console.WriteLine("Drawing a 3D Circle");
}
You can also apply the new keyword to any member type inherited from a base class (field, constant,
static member, or property). As a further example, assume that ThreeDCircle wants to hide the inherited
PetName property:
class ThreeDCircle : Circle
{
// Hide the PetName property above me.
public new string PetName { get; set; }
// Hide any Draw() implementation above me.
public new void Draw()
{
Console.WriteLine("Drawing a 3D Circle");
}
}
Finally, be aware that it is still possible to trigger the base class implementation of a shadowed
member using an explicit cast, as described in the next section. For example, the following code shows:
static void Main(string[] args)
{
...
// This calls the Draw() method of the ThreeDCircle.
ThreeDCircle o = new ThreeDCircle();
o.Draw();
}
// This calls the Draw() method of the parent!
((Circle)o).Draw();
Console.ReadLine();
Source Code The Shapes project can be found under the Chapter 6 subdirectory.
Understanding Base Class/Derived Class Casting Rules
Now that you can build a family of related class types, you need to learn the rules of class casting
operations. To do so, let’s return to the Employees hierarchy created earlier in this chapter. V