What Is Dynamic Binding?
Dynamic binding refers to determining the actual type of the referenced object during execution (non-compilation), and calling its corresponding method according to its actual type. In the process of program running, the process of combining the function (or procedure) call with the code required to respond to the call is called dynamic binding.
- Associate a method with its class / object is called method binding. Binding is divided into static binding (early binding) and dynamic binding (late binding). Static binding (early binding) refers to the fact that the method belongs to that class before the program runs, and can be linked to the class at compile time to locate this method. Dynamic binding (late binding) refers to the method that can be determined specifically based on the specific instance object during the running of the program.
- Static binding occurs between data structures and data structures before the program is executed. Static binding occurs at compile time, so it cannot take advantage of any runtime information. It targets function calls and function bodies, or variables and blocks in memory. Dynamic binding is for access requests generated at runtime, and only uses information available at runtime. in
- Dynamic binding (late binding) refers to: during the program running process, which method can be determined specifically according to the specific instance object.
- Dynamic binding is an important factor in the realization of polymorphism. It is implemented through method tables: When each class is loaded into the virtual machine, metadata is stored in the method area, including a thing called method table The table records pointers to the methods defined by this class, and each entry points to a specific method code. If this class overrides a method in the parent class, the corresponding entry points to the new code implementation. Methods inherited from the parent class are placed before the methods defined by the child class.
- We assume Fatherft = newSon (); ft.say (); Son inherits from Father and overrides say ().
Dynamic binding compilation
- We know that during the upward transformation, the parent class reference is used to execute the child class object, and the parent class reference can be used to call the overridden method of the same name in the child class. But you cannot call the new methods in the subclass.
- During the compilation phase of the code, the compiler looks for a matching method in the method table of that type in the method area by declaring the type of the object (that is, the type of the reference itself). If it compiles, it passes. (The search is based on the declared object type, so here is the method table of the Father class, and there is no new method of the subclass in the method table of the Father class, so it cannot be called.)
- The compilation phase is to ensure the existence of the method and ensure that the program can run smoothly and safely. [2]
Dynamic binding operation
- ft.say () calls the say () in Son, here is the real embodiment of the dynamic binding mechanism.
- The compilation phase looks for methods in the method table that declares the type of the object, just to safely compile (and to verify that the method exists). When this statement is actually run, a Son instance object is created during the execution of Fatherft = newSon () ;, and then when the method is called by ft.say (), the JVM pushes the previous son object onto the operand stack And use it to make calls. The process of invoking a method with an instance object is dynamic binding: it searches its method table according to the type of the instance object, and finds a matching method to call. We know that if the method of the parent class is overridden in the child class, the entry of the same name in the method table will point to the method code of the child class; if there is no overwriting, it will be stored in the method table of the child class in the order of the method table in the parent class in. Therefore: the method of dynamic binding based on the type of the object table lookup method will definitely match (because the compile time is found in the method table of the parent class and the lookup and match are successful, it means that the method exists. This also explains why the parent The class reference cannot call the new method of the subclass: the existence of this method must be checked in the method table of the parent class. If it is checked at runtime, it is easy to be dangerous-this method may not be available in the subclass). [2]
Dynamic binding distinction
- When the program is running in the JVM, the class type information, static properties and methods, and final constants are loaded into the method area. These are already known when the class is loaded, and can be accessed without the creation of objects. Content that is statically bound; you need to wait for the object to be created, and use it based on the type of the instance object in the heap. What is used is the dynamically bound content. [2]