Look at the code first:
Public classSuperclass {} Public classSubclassextendsSuperclass {} Public Static voidMain (string[] args) {Superclass Sub=Newsubclass (); Superclass SUP=Newsuperclass (); Test (sub); Test (SUP); } Public Static voidTest (Superclass SC) {System.out.println ("Parent Class"); } Public Static voidTest (Subclass SC) {System.out.println ("Sub-class"); }
The output is all
Parent Class Parent Class
The above superclass is called the static type, the latter subclass is called the actual type, the static type and the actual type can have some changes in the program, the difference is that the static type changes only when using, the variable itself static type will not be changed, And the final static type is known at compile time, and the result of the actual type change can be determined at runtime, and the compiler does not know what the actual type of an object is when compiling the program. When the compiler is overloaded with a static type of arguments rather than the actual type, the compiler determines which overloaded version to use, depending on the static type of the parameter in the compilation phase.
All allocations that rely on static types to locate the execution version of the method are called static allocations, and the typical static dispatch is the method overload. Static dispatch occurs during the compilation phase, so determining the static dispatch action is not actually performed by the virtual machine. In addition, although the compiler can determine the overloaded version of the method, in many cases this overloaded version is not "unique", often can only determine a "more appropriate" version. When the overloaded parameter does not have a positive corresponding type, a boxing operation may occur and then the "more appropriate" version will be re-selected.
Dynamic allocation and override are related.
[Reading notes] static assignment of Java