##Overload of java (Recommended learning: java course )
Overloading (overloading) is in a class, the method name is the same, but the parameters are different. The return types can be the same or different.
Each overloaded method (or constructor) must have a unique parameter type list.
The most commonly used place is the overloading of the constructor.
Overloading rules:
The overloaded method must change the parameter list (the number or type of parameters is different); The overloaded method can change the return type;The overloaded method can change the access modifier;The overloaded method can declare new or wider checked exceptions;Methods can be overloaded in the same class or in a subclass. The return value type cannot be used as the criterion for distinguishing overloaded functions.Example
public class Overloading { public int test(){ System.out.println("test1"); return 1; } public void test(int a){ System.out.println("test2"); } //以下两个参数类型顺序不同 public String test(int a,String s){ System.out.println("test3"); return "returntest3"; } public String test(String s,int a){ System.out.println("test4"); return "returntest4"; } public static void main(String[] args){ Overloading o = new Overloading(); System.out.println(o.test()); o.test(1); System.out.println(o.test(1,"test3")); System.out.println(o.test("test4",1)); } }
The above is the detailed content of What is method overloading in java. For more information, please follow other related articles on the PHP Chinese website!