7.11 Method hiding with the new keyword


7.11 Method hiding with the new keyword

Please make sure that you understand section 7.10 before you read this section.

Name hiding in C# will be a new concept to Java developers. In C#, the new keyword can be used for two purposes, the first of which is well understood :

  • as an operator “ to create a new instance of a class or struct;

  • as a modifier “ to create a new class member (method, field, constant, property, type) which hides an inherited member from a superclass.

I will concentrate on the second use in this section.

You can use the new keyword to hide a class member. In this case, we shall be concentrating on method hiding (hence the name of this section). However, the same idea can be applied to other class members (property hiding, field hiding, etc.).

Examine the program which follows . It shows two classes A and B “ both contain the DoThis() method, and B is a subclass of A .

 1: using System;  2:  3: class A{  4:   public  virtual  void DoThis(){  5:     Console.WriteLine("A");  6:   }  7: }  8:  9: class B:A{ // B extends A 10:   public  override  void DoThis(){ 11:     Console.WriteLine("B"); 12:   } 13: } 14: 15: class TestClass{ 16:   public static void Main(){ 17: 18:     A temp1 = new A(); 19:     temp1.DoThis(); // output: A 20: 21:     B temp2 = new B(); 22:     temp2.DoThis(); // output: B 23: 24:  A   temp3  =  new B();  25:  temp3.DoThis();  // output: B 26: 27:  A   temp4  =  (A)temp2;  28:  temp4.DoThis();  // output: B 29:   } 30: } 

Output:

 c:\expt>test A B  B   B  

Lines 1 “ 13 give a proper example of method overriding in C#. You need to use the virtual and override keywords. Now examine the output results. The output from lines 19 and 22 is expected “ I have included it in the example for completeness. Examine line 24 in which a new instance of the B class is created, and referenced via a variable of type A . Regardless of the variable's type, when DoThis() on the instance is invoked, the overridden one executes. [16] The output from line 28 simply serves to reinforce the same idea. Regardless of what supertype the instance of B is cast into, the overridden DoThis() method coded in class B runs.

[16] From within class B , you can invoke the overridden method in A by using the base keyword (as in base.DoThis() ) in the overriding DoThis() method.

What happens if you omit the virtual and override keywords in C#? Replacing lines 1 “ 13 with the following still gives a successful compilation (with a warning) that produces a different output altogether.

 1: using System;  2:  3: class A{  4:  public void DoThis()  {  5:     Console.WriteLine("A");  6:   }  7: }  8:  9: class B:A{ 10:  public void DoThis()  { 11:     Console.WriteLine("B"); 12:   } 13: } 

Compilation warning:

[View full width]
 
[View full width]
test.cs(10,15): warning CS0108: The keyword new is required on 'B.DoThis()' because it graphics/ccc.gif hides inherited member 'A.DoThis()'

Output:

 c:\expt>test A B  A   A  

Despite the compiler warning, compilation continues to completion to produce test.exe . What the compiler warning says is that because DoThis() in class A (line 4) hasn't been declared with the virtual modifier, it is not a method which can be overridden. (If you insert the override modifier on line 10 in an attempt to perform an override, you will get a compilation error, not a warning.) Hence, what we are doing here is 'method hiding' in which the new method declared in class B (lines 10 “ 12) hides the old method of the same name in the superclass.

What this means is that the data type of the variable used to reference the object is taken into consideration when determining which of the two methods to invoke. As demonstrated, if the reference variable is of type A , it doesn't matter if the actual instance being referred to is of class A or B , the DoThis() method which is coded in A is invoked. It hasn't been overridden in B , simply hidden.

The proper way to perform method hiding is to use the new keyword. Replace line 10 with this:

 10:   public  new  void DoThis(){ 

and there will be no compiler warning. The compiler is assured that you know that you are performing method hiding rather than method overriding.

Additional notes

  • A class member cannot be declared with both the override and new modifiers. They are antagonistic.

  • A new point of specialization is created when new and virtual are used together. This means if you write another class C , which subclasses B , C can contain another DoThis() method of the same signature which either overrides the method in B , or hides the method in B .

    Examine these three classes:

     class A {public void DoThis(){}} class B:A{public  virtual new  void DoThis(){}} class C:B{public  override  void DoThis(){}} 

    In this case, B is hiding A 's method, and C is overriding B 's method.

    Now examine these three classes:

     class A {public void DoThis(){}} class B:A{public  new  void DoThis(){}} class C:B{public  new  void DoThis(){}} 

    Here we have B hiding A 's method, and C hiding B 's method again. If an instance of C is cast into type A , invoking DoThis() on this object will execute A 's method “ not B 's or C 's.

Method overriding in Java

For those who are not very sure of what will happen during method overriding in Java, I have included the next example in Java as a reference. Remember that Java does not have this method hiding feature, only method overriding.

 1: // Test.java  2:  3: class A{  4:   public void doThis(){  5:     System.out.println("A");  6:   }  7: }  8:  9: class B extends A{ 10:   public void doThis(){ // properly overridden 11:     System.out.println("B"); 12:   } 13: } 14: 15: public class Test{ 16:   public static void main(String args[]){ 17:     A temp1 = new A(); 18:     temp1.doThis(); 19: 20:     B temp2 = new B(); 21:     temp2.doThis(); 22: 23:     A temp3 = new B(); 24:  temp3.doThis();  25: 26:     A temp4 = (A)temp2; 27:  temp4.doThis();  28:   } 29: } 

Output:

 c:\expt>java Test A B  B   B  


From Java to C#. A Developers Guide
From Java to C#: A Developers Guide
ISBN: 0321136225
EAN: 2147483647
Year: 2003
Pages: 221
Authors: Heng Ngee Mok

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