Information Technology Reference
In-Depth Information
Hiding Members of a Base Class
Although a derived class cannot delete any of the members it has inherited, it can hide them.
You can
hide
, or
mask
, an inherited function member by declaring a new function mem-
ber with the same signature. Remember that the
signature
consists of the name and
parameter list, but does not include the return type.
To hide an inherited data member, declare a new member of the same type and with the
same
name
.
To let the compiler know that you are purposely hiding an inherited member, use the
new
modifier. Without it, the program will compile successfully, but the compiler will
give you a warning that you are hiding an inherited member.
The following code declares a base class and a derived class, each with a
string
member
called
Field1
. The keyword
new
is used to explicitly tell the compiler to mask the base class
member. Figure 7-4 illustrates an instance of each class.
class SomeClass // Base class
{
string Field1;
...
}
class OtherClass : SomeClass // Derived class
{
new string Field1; // Mask base member with same name.
↑
Keyword
Figure 7-4.
Hiding a member of a base class