The execution of a Java program requires two steps: compilation and execution (interpretation), and Java is an object-oriented programming language. When the subclass and the parent class have the same method, the subclass overrides the parent class method. When the program calls the method at runtime, whether the method of the parent class is called or the subclass is overridden, this should be a problem we encounter when we are beginning to learn Java. Here we first determine which method to implement or variable operation to determine this call is called binding.
There are two binding methods in Java, one is static binding, also known as early binding. Another type is dynamic binding, also known as late binding.
The concept of program binding:
Binding refers to the association of a method call with the class (the method body) where the method is located. For Java, binding is divided into static binding and dynamic binding; or it is called early binding and late binding.
Static binding (early binding compiler binding):
The method has been bound before the program is executed and is implemented by the compiler or other connecting program. For example: C. For Java, it can be understood as binding during the program compilation period; specifically, the only methods in Java are final, static, private and constructors that are pre-binding
Dynamic binding (late binding during runtime):
Later binding: binds according to the type of the specific object at runtime.
If a language implements late binding, it is necessary to provide some mechanisms to determine the type of the object during operation and call appropriate methods separately. In other words, the compiler still does not know the object type at this time, but the method calling mechanism can investigate it by itself and find the correct method body. Different languages have different ways of implementing late binding. It can be thought that they all need to insert certain special types of information into the object.
Dynamic binding process:
Summary about binding:
After understanding the concept of the three, we found that java belongs to late binding. In Java, almost all methods are late binding, and dynamically binding methods are subclasses or base classes at runtime. But there are also special features. Since static methods and final methods cannot be inherited, their values can be determined at compile time. They belong to the early binding. To be particularly noted, the methods and member variables declared by private cannot be inherited by subclasses, and all private methods are implicitly specified as final (from this we know that the method declared as final type is to prevent the method from being overwritten, and to effectively close dynamic binding in java). Post-binding in Java is implemented by the JVM. We don’t need to explicitly declare it, but C++ is different. We must explicitly declare that a method has post-binding. Upward transformation or polymorphism in Java is achieved through dynamic binding, so understanding dynamic binding means upward transformation and polymorphism.
For methods in Java, except for final, static, private and constructors that are early binding, all other methods are dynamic binding. The typical dynamic binding occurs under the conversion declaration of the parent class and the child class:
For example: Parent p = new Children();
The specific process is as follows:
1. The compiler checks the declaration type and method name of the object. Suppose we call the xf(args) method and x has been declared as an object of class C, then the compiler will list all methods named f in class C and methods inherited from the superclass of class C.
2. Next, the compiler checks the parameter types provided in the method call. If one parameter type among all methods with the name f matches the parameter type provided by the call the most, then this method is called. This process is called "overload parsing"
3. When the program runs and uses dynamic binding to call methods, the virtual machine must call a method version that matches the actual type of the object pointed to by x. Assuming that the actual type is D (a subclass of C), if class D defines f (String), then the method is called, otherwise the method f (String) will be searched in the superclass of D, and so on.
Question Thoughts:
How to provide a method user with a method to complete a task. If the user has special requirements, he or she needs to customize his or her own method?
Knowledge involved:
Child-parent class, interface, upward transformation, dynamic binding
Specific code:
package com.chengxuyuanzhilu;public interface MyInterfaces { void doting();}package com.chengxuyuanzhilu;public class Drink implements MyInterfaces { @Override public void doting() { System.out.println("I'm drinking water"); }}package com.chengxuyuanzhilu;public class Eat implements MyInterfaces { @Override public void doting() { System.out.println("I'm eating"); }}package com.chengxuyuanzhilu;public class Run implements MyInterfaces { @Override public void doting() { System.out.println("I'm running"); }}package com.chengxuyuanzhilu;public class TestDynamicBind { public static void main(String[] args) { MyInterfaces my = null; my = new Eat(); bind(my); my = new Drink(); bind(my); my = new Run(); bind(my); } static void bind(MyInterfaces my){ my.doting(); }}The above is all about this article, I hope it will be helpful to everyone's learning.