New and override in C #

Source: Internet
Author: User

What is the point of using the new and override keywords in a method on a derived class, you can find the answer through a series of questions. Look at the code first:

1     class Program2     {3         Static voidMain (string[] args)4         {5Child C =NewChild ();6 C.func ();7 8 Console.read ();9         }Ten     } One  A     classParent -     { -          Public voidFunc () the         { -System.Console.WriteLine ("Parent"); -         } -     } +  -     classchild:parent +     { A}

  Issue 1: in the code above, the subclass inherits the parent class, so you can call the Func method with the subclass object, and the output string is parent. What happens if you add the same method func to the parent class in the subclass? To modify the subclass code as follows, the generated code is successful, but there is a warning that the Func method of the child class hides the Func method of the parent class, and if it is intentionally hidden, use the keyword new.

1     class child:parent 2     {3public         void  Func ()4        { 5             System.Console.WriteLine ("child"); 6         }7     }

This issue leads to the new keyword, but it does not seem to use the new keyword to hide the parent class method, first with the New keyword elimination Warning, the subclass code is modified as follows, the build succeeds without warning, and the output string is child.

 1  class   Child:parent  2   { 3  public  new  void   Func ()  4   { 5  System.Console.WriteLine ( " child  "   6   7
      } 

Question 2: when the subclass object is assigned to a subclass variable, the new keyword hides the parent class method, which does not seem to have much significance, and the new keyword can be omitted, so when the class object is assigned to a parent class variable, what is the result of calling the Func method? Modify the Portal Main method as follows

1 Static void Main (string[] args)2{3     new Child (); 4     C.func (); 5 6     console.read (); 7 }

When the output string is parent and not the child of the output, then the hiding of the new keyword is only assigned to the subclass variable for the subclass object.

Question 3: So how do you invoke the Func method of a subclass implementation by assigning a child class object to a parent class variable? At this point, you need to use the Override keyword to change the subclass Func method's new keyword to override, and the modified subclass code is as follows

 1  class   Child:parent  2   { 3  public  override  void   Func ()  4   { 5  System.Console.WriteLine ( " child  "   6   7
      } 

The

Generates a Times error that indicates that the Func method of the parent class is not marked as vitual, abstract, or override and cannot be overridden, so overriding the parent class method with override is necessary if the parent class method must be a virtual method, an abstract method, or a parent class override method (Overriding the ... )。 Now modify the func of the parent to be a virtual method, the code is as follows

1 class Parent 2 {3      Public Virtual void Func () 4     {5         System.Console.WriteLine ("parent"); 6     }7 }

The build succeeds, and the output string is child, so from the result it seems that override is the true "hidden" parent method.

Summarize:

(1) Assigning the object of a class to a variable of that class, the variable calls must be a member of that object, so the new keyword does not feel very large, and the compiler even allows it to be omitted;

(2) When you assign a subclass object to a parent class variable, the variable calls the subclass method implementation after using override overrides.

New and override in C #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.