Java Method overloading

Source: Internet
Author: User

What is an overload of a method: method overloading is a means for a class to handle different types of data in a uniform manner. Multiple functions with the same name exist at the same time, with different number/types of parameters. Overloaded overloading is a representation of polymorphism in a class. Simply put, the overloading of a method is a multiple implementation of the same function of a class. Which method to use depends on the parameters passed at the time of invocation   Assuming that we have a requirement to write a method to receive two parameters and return the larger number. That's what we're going to write.//If the parameter is of type int getmaxint (int a, int b) {if (a > B) {return A;} else {return B;}  } //If the parameter is a float type of public float getmaxfloat (float A, float b) {if (a > B) {return A;} Else{return b;}} Getmaxint is called when we want to compare the larger int data. To get a larger float, call getmaxfloat. Can you define two method names as well, such as uniformly defined as a Getmax method name? Because two methods use the same method name, but because of their return data type, and the type of the parameter is different, so the Java compiler can identify you exactly is called which, this is a polymorphic embodiment of  public int getmax (int A, int b) {if (a > B) {return A;} else {return B;}  } //If the parameter is a float type of public float getmax (float A, float b) {if (a > B) {return A;} Else{return b;}}   Method overload precautions: 1. The parameter type of the method, the number of parameters (the parameter name does not count), the order of at least one of the different 2. The returned data type can be 3 different. The modifier of the method can be different   if just the return data type is not the same can you make an overload? For example:  public float Getmax (float A, double) { if (a > B) {}return (float) A;} else {return (float) b;}  public Double Getmax (float A, float b{ if (a > B) {}return A;} else {return B;}   The answer is: If just the return data type is different and does not constitute an overload   if just the access modifier is not the same, can you make an overload?   The answer is: no  

Java method overloading

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.