Information Technology Reference
In-Depth Information
In the following code, OtherClass derives from SomeClass but hides both its inherited
members. Note the use of the new modifier. The code is illustrated in Figure 7-5.
class SomeClass // Base class
{
public string Field1 = "SomeClass Field1";
public void Method1(string value)
{ Console.WriteLine("SomeClass.Method1: {0}", value); }
}
class OtherClass : SomeClass // Derived class
{ Keyword
new public string Field1 = "OtherClass Field1"; // Mask the base member.
new public void Method1(string value) // Mask the base member.
{ Console.WriteLine("OtherClass.Method1: {0}", value); }
} Keyword
class Program
{
static void Main()
{
OtherClass oc = new OtherClass(); // Use the masking member.
oc.Method1(oc.Field1); // Use the masking member.
}
}
Figure 7-5. Hiding a field and a method of the base class
Search WWH ::




Custom Search