The concept of binding:
Binding refers to the invocation of a method associated with the class (method body) in which the method resides. For Java, bindings are divided into static and dynamic bindings, or pre-and late-binding.
Static bindings:
The method is already bound before the program executes, and is implemented by the compiler or other linker.
For Java simple can be understood as the program compile time of the binding, here specifically, the Java method only Final,static,private and construction method is pre-binding
Dynamic binding:
Binds at run time based on the type of the specific object.
If a language implements dynamic binding, it must also provide mechanisms to determine the type of the object during runtime and to invoke the appropriate method, respectively. That is, the compiler still does not know the type of the object at this point, but the method invocation mechanism can investigate itself and find the correct method body. Different languages have a difference in how late binding is implemented. But at least we can think of it: they all have special types of information in the object.
the process of Java dynamic binding:
- The compiler looks at the declaration type and method name of the object. Assume that you call X.F (param), and that the implicit parameter x is declared as an object of Class C. The compiler will enumerate through all the methods named F in the Class C and the method named F whose parent class has access to the public property.
At this point, the compiler has obtained all the candidate methods that might be called.
- Next, the compiler looks at the type of arguments that are provided when the method is called. Select this method if there is an exact match to the supplied parameter type in all methods named F. This process is called overload resolution (Overloadingresolution). An error is reported if the compiler does not find a method that matches the type of the parameter, or if more than one method has been found to match the type conversion.
At this point, the compiler has obtained the method name and parameter type that need to be called.
- if it is a private method, a static method, a final method, or a constructor, then the compiler will be able to know exactly which method should be called, and we will call this called static binding. By contrast, the method that is called relies on the actual type of the implicit parameter and is dynamically bound at run time.
- When the program is running and the method is called with dynamic binding, the virtual machine must call the method of the class that best fits the actual type of the object referenced by X. Assume that the actual type of x is D, which is a subclass of Class C. If the class D defines method F (string), it is called directly; otherwise, F (string) is found in the superclass of Class D, and so on.
Summarize:
In Java , almost all methods are dynamically bound, whether the dynamic binding method is a subclass or a parent class at run time. But there are also special, for static methods and final methods because they cannot be inherited, so at compile time you can determine their values, they are statically bound. Specifically, the method and member variables of the private declaration cannot be inherited by the quilt class, and all private methods are implicitly specified as final (we can also know that the method is declared as one of the final types to prevent the method from being overwritten, and the second is to effectively close dynamic Binding in Java).
Java Dynamic binding static bindings