The difference between overriding (override) and overloading (overload)

Source: Internet
Author: User

1). Overwrite (override)

Override is overriding (overriding) a method to implement different functions, typically for subclasses that override (re-implement) the parent class when inheriting the parent class, implementing different requirements.

Override (Overwrite) the rule:

1. The overridden method must be either Virtual,abstract or override (the virtual modifier cannot be used with the static,abstract,private or override modifier. Using the virtual modifier on a static property will cause an error).

2. The parameter list of the overridden method must be exactly the same as the overridden method, otherwise it cannot be called overriding but overloaded.

3. The access modifier for the overridden method must be greater than the access modifier (public>protected>default>private) of the overridden method.

4. The return value of the overridden method must be the same as the returned value of the overridden method.

5. The overridden method throws an exception that must be the same as the exception thrown by the overridden method, or its subclass (eg: a method of the parent class declares a check exception IOException, the method cannot be thrown exception, only the subclass exception of IOException is thrown. , you can throw a non-check exception).

6. The overridden method cannot be private, otherwise it simply defines a new method in its subclass and does not override it.

7. Static methods cannot be overridden as non-static methods (compile error).

8. Subclasses cannot override the final decorated method in the parent class.

1 /// <summary>2         ///Energy-based animal class3         /// </summary>4         /// <returns></returns>5          Public classAnimal6         {7              Public Virtual voideatting ()8             {9Console.WriteLine ("eat with your mouth! ");Ten             } One         } A  -         #regionOverride (Overriding principle) -  the          Public classPople:animal -         { -              Public Override voidEatting ()//the base class must be a virtual method -             { +Console.WriteLine ("people eat with chopsticks! "); -             } +         } A  at          Public classMonkey:animal -         { -              Public New voideatting () -             { -Console.WriteLine ("monkeys eat directly with their hands! "); -             } in         } -  to  +         #endregion -  the         /// <summary> *         ///differences between method overloads and write writes $         /// </summary>Panax Notoginseng         /// <param name= "args" ></param> -          Public Static voidMain (string[] args) the         { +             varPople =Newpople (); A(Pople asAnimal). Eatting ();//print "People eat with chopsticks!" " thePople. Eatting ();//print "People eat with chopsticks!" " +  -             varMonKey =NewMonkey (); $(MonKey asAnimal). Eatting ();//print "Eat with your mouth!" " $Monkey.eatting ();//print "Monkey eat directly by hand!" " -  - Console.readkey (); the}
Demo Code Demo

2). Reload (overload)

  Overload are overloads, which are generally used to implement several overloads in a class, and the names of these methods differ by the same parameters.

Rules for overloading:

1. The overloads can only be implemented with the same method name and different parameter form classes. Different parameter types can be different parameter types, different parameter numbers, different parameter order (parameter types must not be the same).

2. Cannot be overloaded by an exception that is thrown by access permission, return type.

3. The asynchronous type and number of methods do not affect overloading.

1 usingSystem;2 usingSystem.Threading.Tasks;3 4 namespaceoverreadoverloaddifference5 {6     class Program7     {8         #regionMethod overloading9 Ten         /// <summary> One         ///defining a Calculation class A         /// </summary> -          Public classCalculation -         { the              Public intSunmation (inta) -             { -                 returnA; -             } +  -              Public intSunmation (intAintb) +             { A                 returnA +b; at             } -         } -  -         #endregion -  -         /// <summary> in         ///Method Overloading -         /// </summary> to         /// <param name= "args" ></param> +          Public Static voidMain (string[] args) -         { the             #regionOverload *             varCalculation =Newcalculation (); $Console.WriteLine (Calculation.sunmation (1));Panax NotoginsengConsole.WriteLine (Calculation.sunmation (2,3));  -             #endregion the  + Console.readkey (); A         } the     } +}
Demo Code Demo

The difference between overriding (override) and overloading (overload)

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.