New Modifier


The new modifier hides a member of the base class. New is the default modifier when a member is replicated in a derived type. However, unless the new keyword is stated explicitly, a compiler warning is presented to prevent the inadvertent hiding of a member of the base class. Explicit use of the new keyword prevents the warning. A virtual member and overridden member are related, which is important to polymorphism. The derived member and hidden member are, however, unrelated.

NET supports hide-by-signature and hide-by-name techniques. C# only supports hide-by-signature, where a single member is hidden using the new modifier. Hide-by-name hides the entire interface of a member, which may entail several functions. This option is available in Visual Basic .NET.

In the following code, ZClass is the base class and contains MethodA and MethodB members. Both are virtual methods, and MethodB calls MethodA.

 public virtual void MethodB() {     MethodA(); } 

YClass is a derived class and inherits from ZClass. MethodA is overridden in the derived type, but MethodB is not overridden. Therefore, two versions of MethodA exist: a base version and a derived version. When YClass.MethodB is called, which version of MethodA is invoked in the method? Because MethodA is virtual, the most derived method is called. YClass.MethodA is invoked. With a virtual method, the compiler prefers the most derived method.

Here is the code:

 using System; namespace Donis.CSharpBook{     public class Starter{         public static void Main(){             YClass obj=new YClass();             obj.MethodB(); // YClass.MethodA         }     }     public class ZClass {         public virtual void MethodA() {              Console.WriteLine("ZClass.MethodA");         }         public virtual void MethodB() {              MethodA();         }     }     public class YClass: ZClass     {         public override void MethodA() {              Console.WriteLine("YClass.MethodA");         }     } } 

The following code is almost identical to the previous code. However, ZClass.MethodA is not overridden in the derived class. Instead, YClass hides the base class implementation of MethodA with the new modifier. ZClass.MethodA and YClass.MethodA are now unrelated. Therefore, the compiler will not delegate to YClass.MethodA for an implementation inherited from the parent. For this reason, YClass.MethodB calls ZClass.MethodA. This prevents functions inherited from the base class in calling unrelated functions, which could cause the fragile base class problem.

 using System; namespace Donis.CSharpBook{     public class Starter{         public static void Main(){             YClass obj=new YClass();             obj.MethodB(); // ZClass.MethodA         }     }     class ZClass {         public virtual void MethodA() {              Console.WriteLine("ZClass.MethodA");         }         public virtual void MethodB() {              MethodA();         }     }     class YClass: ZClass     {         public new void MethodA() {              Console.WriteLine("YClass.MethodA");         }     } } 

Interestingly, although the new modifier hides the base class member, you can still access the base class implementation with the base keyword. This is demonstrated in the following code:

 public class ZClass {      public virtual void MethodA() {          Console.WriteLine("ZClass.MethodA");     } } public class YClass: ZClass {     public new void MethodA() {         base.MethodA();         Console.WriteLine("YClass.MethodA");     } } 

Virtual methods are overridable in all descendants. The new modifier ends the propagation of this characteristic. The new modifier changes virtual methods to nonvirtual. The method cannot be overridden in further descendants. A function can be declared as both new and virtual. In that circumstance, the function hides the base method and is overridable to descendants.

The following code demonstrates that a member with the new modifier cannot be overridden:

 public class ZClass {     public virtual void MethodA() {     }     public virtual void MethodB() {     } } public class YClass: ZClass {     public new virtual void MethodA() {     }     public new void MethodB() {     } } public class XClass: YClass {     public override void MethodA() {     }     /* ERROR     public override void MethodB() {     }     */ } 

Base fields and static members cannot be overridden in a derived class. However, both can be hidden with the new modifier. Hiding field and static members can cause confusion. When you use this feature, I recommend thorough supporting documentation in your code.

In the following code, ZClass has a static method and field. Their purpose is to count ZClass instances. YClass inherits ZClass and hides the two static members of the base type. The new members count the instances of the YClass. Therefore, there are simultaneous counts—the base class and derived class counters. In Main, multiple instances of the ZClass and YClass are created. Both counters are then displayed.

 using System; namespace Donis.CSharpBook{     public class Starter{         public static void Main(){             ZClass obj1=new ZClass();             YClass obj2=new YClass();             YClass obj3=new YClass();             ZClass.DisplayCounter();             YClass.DisplayCounter();         }     }     public class ZClass {         public ZClass() {             ++count;         }         public static int count=0;         public static void DisplayCounter() {              Console.WriteLine("ZClass.Count:");              Console.WriteLine(count);         } /*      ERROR - static methods cannot be virtual         public static virtual void DisplayCounter() {              Console.WriteLine("ZClass count:");              Console.WriteLine(count);         } */     }     public class YClass: ZClass     {         public YClass() {             ++count;         }         private new static int count=0;         public new static void DisplayCounter() {              Console.WriteLine("YClass count:");              Console.WriteLine(count);         }     } } 




Programming Microsoft Visual C# 2005(c) The Language
Microsoft Visual Basic 2005 BASICS
ISBN: 0619267208
EAN: 2147483647
Year: 2007
Pages: 161

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