Override (override) and new (overwrite) usages in C # inheritance

Source: Internet
Author: User

Just in touch with C # programming, I was also bewildered by the override and new. So took a little time to turn over the information, read the blog, and finally a small understanding, the study notes recorded here.

First declare a parent class animal class, and inherit the animal two subclass of the dog class with the Cat class. There is a say method in the parent class animal, and the subclass Dog and Cat override (override) and new (overwrite) the Say method.

Let's take a look at some of the similarities and differences between the two.

1      Public classAnimal2     {3          Public Virtual voidSay ()4         {5Console.WriteLine ("Hello,Animal");6         }7     }8      Public classDog:animal9     {Ten          Public Override voidSay () One         { AConsole.WriteLine ("Hello, Dog"); -         } -     } the      Public classCat:animal -     { -          Public New voidSay () -         { +Console.WriteLine ("Hello, Cat"); -         } +}

First, the similarities between override and new:

    1. are used when a subclass has the same method of signing in the parent class when it is re-implemented.
    2. When declaring a subclass object, call this method, which is the method implemented in the subclass.

For example:

1 class Program2     {3         Static voidMain (string[] arge)4         {5Dog d =NewDog ();6Cat C =NewCat ();7 D.say ();//Call the override method8 C.say ();//method to call new9         }Ten}

The output is:

Hello,doghello,cat

This is called the Say method implemented in the dog and cat classes.

3. None of the methods of the parent class itself will be affected.

Such as:

1 class  Program 2     {3         staticvoid Main (string[] arge)4         {5             new  Animal (); 6             A.say (); // Call the parent class method. Not affected.  7        }8     }

The output at this time is:

Hello,animal

Here are the differences between the two:

1.

(1) Override: The parent method must be modified with virtual to indicate that the method is a virtual method and can be overridden. Otherwise, it cannot be overridden.

(2) NEW: The parent class method does not have to use virtual adornments.

2.

(1) Override: When using override, there must be a virtual method in the parent class with exactly the same signature. Otherwise the compilation does not pass.

If I add a string-type parameter to the say of the dog class, the compiler prompts: there is no proper way to override it.

(2) NEW: When using new, it is best to have the same signature method in the parent class. If not, VS will have a hint, but the compilation will not error. At this point, the new keyword has no meaning.

If I add a string type parameter to the say of the cat class, vs will prompt: The new keyword is not required.

3. The default is new when a subclass has the same method as the parent method signature, but not override or new.

That is, override must be written, and new may not be written (not recommended).

4. This is the most important point. The above three points are the difference between the use methods, and this is the difference between the two in the actual use of the effect.

(1) Override: When overridden, the overridden virtual method cannot be accessed when the subclass object is converted to a parent class. That is, when the quilt class overrides, the virtual method is invalidated in this subclass.

Such as:

class program    {        staticvoid Main (string[] arge)        {            new  Dog () ;              as Animal; // the child class is converted to a parent class. Note that at this point A and D point to the same object, but D is accessed as the dog class, and a is accessed as the animal class            D.say ();   This is called the override method            A.say ();   The override method is called at this time         }    }

The output is:

Hello,doghello,dog

Two calls are say methods that are overridden in the dog.

(2) NEW: After overwriting, the overridden parent class method can be accessed when the subclass object is converted to a parent class. That is, after the conversion to a parent class, the method of the subclass new is invalidated, and the parent method is called.
When it is re-converted to a subclass, the call becomes a subclass method.

Such as:

1 class Program2     {3         Static voidMain (string[] arge)4         {5Cat C =NewCat ();6Animal A = C asAnimal;//the child class is converted to a parent class. Note that at this point A and C point to the same object, but C is accessed as a cat class, and A is accessed as a animal class7C.say ();//The new method is called at this time8A.say ();//The method in the parent class is called at this time9         }Ten}

The output at this time is:

Hello,cathello,animal

Memory principle:
We all know that the method of invoking an object is actually an address in memory that accesses an object method. So since we can pass C. Say () accesses the method of the parent class, stating that in Object C there is also a Say method for the animal class.
In fact, when a subclass inherits from the parent class, all methods, fields (including private) in the parent class are copied and placed in the subclass. And what we call rewriting and overwriting, overriding, overwriting a method that is stored in a subclass, rather than a method in the parent class, does not, of course, have any effect on the parent class. You cannot call a private, or overridden, method or field because you do not have permission to access it, not in memory.

Override (override) and new (overwrite) usages in C # inheritance

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.