9.Java Method Overloading

Source: Internet
Author: User

In Java, multiple methods in the same class can have the same name, as long as their argument lists are different, which is called method overloading.

Parameter lists are also called parameter signatures, including the type of the parameter, the number of arguments, and the order of the parameters, as long as there is a difference between the parameter list.

Overloading is a fundamental feature of object-oriented.

Let's look at a detailed example below.

  1. Public class Demo{
  2. //An ordinary method with no parameters
  3. void Test(){
  4. System. Out. println("No parameters");
  5. }
  6. //Reload the above method with an integer parameter
  7. void Test(int a){
  8. System. Out. println("A:" + a);
  9. }
  10. //Reload the above method, and with two parameters
  11. void Test(int a,int b){
  12. System. Out. println("A and B:" + a + "" + b);
  13. }
  14. //Reload the method above, and with a double-precision parameter
  15. Double test(double A){
  16. System. Out. println("Double A:" + a);
  17. return a*a;
  18. }
  19. public static void main(String args[]){
  20. demo obj= New demo();
  21. Obj. Test();
  22. Obj. Test(2);
  23. Obj. Test(2,3);
  24. Obj. Test(2.0);
  25. }
  26. }

Operation Result:
No parameters
A:2
A and B:2 3
Double a:2.0

From the above example, the reader can see that overloading is a function in a class that has the same function name, but with different parameters. The result of overloading allows a program segment to minimize the kinds of code and methods.

Description

    • Different parameter lists include: different numbers, different types, and different order.
    • It is not possible to have only parameter variable names.
    • As with member methods, construction methods can also be overloaded.
    • A method that is declared final cannot be overloaded.
    • A method declared as static cannot be overloaded, but can be declared again.


Rules for overloading of methods:

    • Method names must be the same.
    • The parameter list must be different (the number is different, or the type is different, the parameters are arranged in different order, etc.).
    • The return type of the method can be the same or different.
    • Only the return type is not sufficient to be an overload of the method.


Implementation of method overloading:
When the method name is the same, the compiler will match the number of parameters of the calling method, parameter type, etc. to select the corresponding method, if the match fails, then the compiler error, which is called overload resolution.

9.Java Method Overloading

Related Article

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.