Inheritance and Name Hiding


It is possible for a derived class to define a member that has the same name as a member in its base class. When this happens, the member in the base class is hidden within the derived class. While this is not technically an error in C#, the compiler will issue a warning message. This warning alerts you to the fact that a name is being hidden. If your intent is to hide a base class member, then to prevent this warning, the derived class member must be preceded by the new keyword. Understand that this use of new is separate and distinct from its use when creating an object instance.

Here is an example of name hiding:

 // An example of inheritance-related name hiding. using System; class A {   public int i = 0; } // Create a derived class. class B : A {   new int i; // this i hides the i in A   public B(int b) {     i = b; // i in B   }   public void show() {     Console.WriteLine("i in derived class: " + i);   } } class NameHiding {   public static void Main() {     B ob = new B(2);          ob.show();   } }

First, notice the use of new in this line:

 new int i; // this i hides the i in A

In essence, it tells the compiler that you know that a new variable called i is being created that hides the i in the base class A. If you leave new out, a warning is generated.

The output produced by this program is shown here:

 i in derived class: 2

Since B defines its own instance variable called i, it hides the i in A. Therefore, when show( ) is invoked on an object of type B, the value of i as defined by B is displayed—not the one defined in A.

Using base to Access a Hidden Name

There is a second form of base that acts somewhat like this, except that it always refers to the base class of the derived class in which it is used. This usage has the following general form:

 base.member 

Here, member can be either a method or an instance variable. This form of base is most applicable to situations in which member names of a derived class hide members by the same name in the base class. Consider this version of the class hierarchy from the preceding example:

 // Using base to overcome name hiding. using System; class A {   public int i = 0; } // Create a derived class. class B : A {   new int i; // this i hides the i in A   public B(int a, int b) {     base.i = a; // this uncovers the i in A     i = b; // i in B   }   public void show() {     // this displays the i in A     Console.WriteLine("i in base class: " + base.i);     // this displays the i in B     Console.WriteLine("i in derived class: " + i);   } } class UncoverName {   public static void Main() {     B ob = new B(1, 2);     ob.show();   } }

This program displays the following:

 i in base class: 1 i in derived class: 2

Although the instance variable i in B hides the i in A, base allows access to the i defined in the base class.

Hidden methods can also be called through the use of base. For example, in the following code, class B inherits class A, and both A and B declare a method called show( ). Inside B’s show( ), the version of show( ) defined by A is called through the use of base.

 // Call a hidden method. using System; class A {   public int i = 0;   // show() in A   public void show() {     Console.WriteLine("i in base class: " + i);   } } // Create a derived class. class B : A {   new int i; // this i hides the i in A   public B(int a, int b) {     base.i = a; // this uncovers the i in A     i = b; // i in B   }   // This hides show() in A. Notice the use of new.   new public void show() {     base.show(); // this calls show() in A     // this displays the i in B     Console.WriteLine("i in derived class: " + i);   } } class UncoverName {   public static void Main() {     B ob = new B(1, 2);     ob.show();   } }

The output from the program is shown here:

 i in base class: 1 i in derived class: 2

As you can see, base.show( ) calls the base class version of show( ).

One other point: notice that new is used in this program to tell the compiler that you know that a new method called show( ) is being created that hides the show( ) in A.




C# 2.0(c) The Complete Reference
C# 2.0: The Complete Reference (Complete Reference Series)
ISBN: 0072262095
EAN: 2147483647
Year: 2006
Pages: 300

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net