Method overload resolution

1, What is method overloading?

        When two or more methods have the same name but different parameter types or numbers, the method is overloaded.

The editor can only determine which method to call by method name and parameter list?

2, Overload matching selection of method

1. Basic type overload

 public static void main(String[] args) {
    short s = 4;
    m(s);
}
 public static void m(int x){//Method 1
    System.out.println("Overload method I");
}
 public static void m(float x){//Method 2
    System.out.println("Overload method II");
}

The result is overloaded method 1. short type can be automatically converted to int type by default. float type, but m(s) really matches m(int x), so the matching rule of the basic type is that if no formal parameter of the exact type is matched, the formal parameter whose storage length is greater than and closest to the actual parameter storage length will be matched first.

2. Overload of reference type

public class Test_3 {
public static void main(String[] args)  {
   Children children = new Children();
   someMethod(children);
}   
public static void someMethod(Ancestor an) {//Overload method 1
    System.out.println("this is Ancestor Method!");
}
public static void someMethod(Parent an) {//Overload method 2
    System.out.println("this is Parent Method!");
}
}
//3 classes with inheritance
class Ancestor{//Ancestral class    
}
class Parent extends Ancestor{//Parent class, inherited from Ancestor  
}
class Children extends Parent{//Subclass, inherited from Parent    
}

The result is: this is Parent Method!    Like the basic type, the reference type selects the "most explicit method". The rule for selecting the most explicit overloaded method among reference types is: if the reference type of the formal parameter of the overloaded method is not found to be consistent with the actual parameter, the actual parameter is preferentially matched with the formal parameter closest to the actual parameter type on the inheritance tree structure

3. Automatic unpacking and packing variable parameter type

public class Test_3 {
public static void main(String[] args)  {
   short s = 5; 
   overloadMethod(s);// test1
   Integer i = 10;
   overloadMethod(i);//test2
   overloadMethod(s,s);//test3
}   
public static void overloadMethod(int a) { //m1
    System.out.println("call  overloadMethod(int)");
}
public static void overloadMethod(Short in) {//m2
    System.out.println("call  overloadMethod(short)");
}
public static void overloadMethod(int a,int b) {//m3
    System.out.println("call  overloadMethod(int,int)");
}
public static void overloadMethod(short... s) { //m4
    System.out.println("call  overloadMethod(short...)");
}
public static void overloadMethod(Integer... i) {//m5
    System.out.println("call  overloadMethod(Integer...)");
}
}

The end result is   Call   Overload method (int), calling   Overload method (int), calling   overloadMethod(int,int)

These formal parameter matching rules are in order:

  • First, find the closest formal parameter list according to the matching rules corresponding to the type (basic type or reference type) of the argument, so as to find the most explicit overload method; if it cannot be found, execute step 2;

  • Carry out boxing or unpacking conversion on the argument (provided that the argument is a basic type or packaging class), and then match the type of the formal parameter according to the converted type (the formal parameter type should be consistent with the conversion type, with special attention to the basic type); if it cannot be found, execute step 3;

  • The matching formal parameter is an overloaded method of variable parameters. At this time, the type of the formal parameter can be the type of the argument and the conversion type obtained through the short to long conversion of the basic type, automatic boxing and unpacking, and ancestor type.

Matching rules for current type (basic type or reference type) > auto packing and unpacking > variable parameter list

4. Overloading of generic methods

Overload rules for generic methods: erase the type variables of generic methods, and then match them one by one according to the above three rules as for non generic methods

public static void main(String[] args)  {
//Create Runnable object
Runnable r = new Runnable() { public void run(){} };
//Call generic method
  m(r); 
}
public static <T> void m(T t) {//m1
    System.out.println("Called<T> void m(T)");
}
public static <T extends Runnable> void m(T t) {//m2
    System.out.println("Called<T extends Runnable> void m(T t)");
}

The result of the run is that the   < T extensions runnable > void m (t t) called

After erasure, the first method is object and the second method is Runnable

The difference between rewriting and overloading

  • Overriding is for methods between parent and child classes, that is, the methods of the parent class must be inherited first. Overloading has no such restriction.

  • Rewriting requires a method, and method overloading requires only the same method name and different parameter lists.

  • When a method is overloaded, the method is called at the compile time. When a method is overridden, it is necessary to determine whether the method of the child class or the parent class is called at run time.

Tags: Java

Posted on Fri, 26 Nov 2021 00:55:40 -0500 by Hangston