Research on the processing strategy when the parameters of the Java overloaded method are parent and child classes

This article first checks the processing strategy when the parameters of the Java overloaded method are parent and child classes, and then further explores the processing strategy after adding rewriting.

1. Cause

The teacher raised such a question:
There are two overload methods in class A:
void B(Object o)
void B(Number n) which B is called by
the client program new A().B(Integer.valueOf(1))
method? Or does this code fail to compile?

2. Running results and reasons

insert image description here
        After the rough completion, the running result is as shown in the figure above, and the method whose parameter is Number n is called. The reason is actually very simple. The Object class is the parent class of all classes, and the Number class is the parent class of Byte, Double, Float, Integer, Long, Short and other classes. In the above program, the parameter passed in by the main program is the return value of Integer.valueOf(1), that is, the Integer type, and the relationship between the three is Object→Number→Integer. A().B(Integer.valueOf(1)) did not find a type-matching method in class A. At this time, the Java automatic type promotion mechanism was triggered. After the upward transformation of Integer, it matched public void B(Number n) method, output the string "Num" after calling.
        We use the String class to test, as expected, as shown in the left picture below. And if there is no String class or its parent class in the method parameter, an error will be prompted during the static inspection phase, as shown in the right figure below.

3. Explore again

        If you are given the following code, what do you think will be the output on the console after running?
        Does anyone have the same answer as I did at the beginning with the following three sentences?
                Obj from C
                Num from C
                Str fromA
        Its final output is actually
                Obj from C
                Obj from C
                Str fromA

package one;
class A {
    
    
	public void B(Object o) {
    
    
		System.out.println("Obj from A");
	}
	public void B(String o) {
    
    
		System.out.println("Str from A");
	}
}
class C extends A{
    
    
	public void B(Object o) {
    
    
		System.out.println("Obj from C");
	}

	public void B(Number n) {
    
    
		System.out.println("Num from C");
	}
}
public class Test{
    
    
	public static void main(String[] args) {
    
    
		Object t1 = null;
		Number t2 = null;
		String t3 = null;
		A a=new C();
		a.B(t1);
		a.B(t2);
		a.B(t3);
	}
}

        The reason for this result is that the reference type of a is class A, but the memory it points to is an instance of class C. When calling a method, the called method must be a method that exists in class A. If the method public void B(Object o) in class A is deleted from the above code, then aB(t1); aB(t2); Each statement will have a static detection error like the previous picture.
        When Java executes a method, it is dynamically matched, and which method to use is determined at runtime. We analyze aB(t2). First of all, it does not find a method whose parameter type is Number in class A, so it performs upward transformation on Number and finds that it matches the public void B(Object o) in A successfully. But a points to the memory space of class C, and the method of public void B (Object o) is overridden in C, so the dynamic link calls the method in class C to output "Obj from C". Here I refer to it when checking According to the predecessors I heard:
        Overload is determined by the compiler at compile time , because the declared type of a is A, so the public void B(Object o) method in class A will be called, and the compiled type is consistent.
        Override is polymorphic, determined by the actual class at runtime , so the method of class C is called instead of the method of class A.
        In this way, the execution results of t2 and t3 are easy to explain and will not be repeated here.

The above is the result of thinking and checking after the teacher asked the question. If there is any mistake, please correct me.

Guess you like

Origin blog.csdn.net/weixin_45630209/article/details/105666003