C# Masking Members of a Base Class


This entry is part 8 of 10 in the series C# Inheritance

A derived class cannot delete any of the members it has inherited; it can, however, mask a base class member with a member of the same name. You might want to inherit from a base class that has a particular method which may not do exactly what you need it to do. You want to mask the base class method with a new member declared in the derived class. You want to “change” the functionality of a base class method.

  • To mask an inherited data member, declare a new member of the same type and with the same name.
  • To mask an inherited function member, declare a new function member with the same signature. Remember that the signature consists of the name and parameter list but does not include the return type.
  • To let the compiler know that you’re purposely masking an inherited member, use the new modifier. Without it, the program will compile successfully, but the compiler will warn you that you’re hiding an inherited member.
  • You can also mask static members.

The book Illustated C# 7, Fifth Edition illustrates. Look at the following code:

using System;
namespace InheritanceMaskingCh8
{
    class BaseClass // Base class
    {
        public string Field1 = "BaseClass Field1";
        public void Method1(string value)
        { Console.WriteLine($"BaseClass.Method1: { value }"); }
    }
    class DerivedClass : BaseClass // Derived class
    {
        // the new keyword tells compiler to mask the base class member.
        new public string Field1 = "DerivedClass Field1"; // Mask the base member.
        new public void Method1(string value)
        { Console.WriteLine($"DerivedClass.Method1: { value }"); }
    }
    class Program
    {
        static void Main(string[] args)
        {
            DerivedClass dc = new DerivedClass(); // Use the masking member.
            dc.Method1(dc.Field1); // Use the masking member.
        }
        // OUTPUT: DerivedClass.Method1: DerivedClass Field1
    }
}

Base Access

If your derived class absolutely must access a hidden inherited member, you can access it by using a base access expression. This expression consists of the keyword base, followed immediately by a period and the name of the member.

using System;
namespace InheritanceBaseAccessCh8
{
    class BaseClass
    {   // Base class
        public string Field1 = "Field1 -- In the base class";
    }
    class DerivedClass : BaseClass
    {   // Derived class
        // Hides the field in the base class:
        new public string Field1 = "Field1 -- In the derived class";
        
    public void PrintField1()
        {
            Console.WriteLine(Field1);      // Access the derived class.
            Console.WriteLine(base.Field1); // Access the base class.
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            DerivedClass dc = new DerivedClass();
            dc.PrintField1();
        }
        // OUTPUT:
        // Field1 -- In the derived class
        // Field1 -- In the base class
    }
}

If you find that your program’s code is frequently using this feature—that is, accessing a hidden inherited member—you might want to reevaluate the design of your classes. Generally there are more elegant designs, but the feature is there if there’s a situation where nothing else will do.

Series Navigation<< C# Abstract ClassC# Inheritance Access Modifiers >>