Method Overloading (overload)

Source: Internet
Author: User

  1. First, there are two meanings of polymorphism in Java, overloading (overload) and overriding (override), which are polymorphism related to inheritance.

  2. The first: The polymorphism of the operation name, that is, there are multiple operations with the same name, but these operation locks must accept different message types.

    The second type: polymorphism related to inheritance, which may have different behavior when the same operation is called by different and type objects.

    Here is a brief introduction to the first polymorphic, a simple understanding of the overloads of the methods in Java:

  3. So what is overload? That is, a class can have multiple methods with the same name, but the parameters of these methods must be different:

    1) The number of parameters is different.

    2) The number of arguments is the same, but the type of one parameter in the argument list is different.

    It is also important to note that the return type of the method and the name of the parameter do not participate in the comparison, that is, if the names of the two methods are the same, the parameters must be guaranteed to be different, even if the return type is different.

  4. Public class people {    public int hello (Int x, int  y) {        return x+y;    }     public float hello (float x, int y) {         return x+y;    }    public double hello ( Double x, int y) {        return x+y;     }}public class testpeople {    /**    *   @author   Shepherd's queen,     *  @param  args    *  @date  2015-10-13    */    public static void main ( String[] args)  {        People p = new  People ();    &nbsP;    system.out.println (P.hello (10, 10));  //20         system.out.println (P.hello (12.12f, 7)); //19.119999         system.out.println (P.hello (12.03, 7));  //19.03     }}

5. In the case of overloading of methods, it is also necessary to avoid the ambiguity of overloading, that is, it is unclear which of the overloaded methods should be executed, so that the program cannot compile.

void cry (double x, int y) {...}   void Cry (int x,double y) {...} p.cry (10,10); A prompt message appears: The reference to cry is ambiguous.

Method 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.