Pre-Java (static) binding and late (dynamic) binding

Source: Internet
Author: User

Pre-Java (static) binding and late (dynamic) binding

Concept of program 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 execution of the program (that is, it is known in the process of compiling the method in which class the method is in), which is implemented by the compiler or other linker. For example: C.
For Java simple can be understood as the program compile time of the binding; In particular, the Java method is only final,static,private and the constructor method is pre-binding

Dynamic binding:
Late binding: binds at run time based on the type of the specific object .
If a language implements late binding, it must also provide mechanisms to determine the type of the object during run time and 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 dynamic binding:

    1. A method table that extracts the actual types of objects from a virtual machine;
    2. Virtual machine Search method signature;
    3. Invokes a method.


Understanding of Final,static,private and construction methods is early binding
For the private method, the first point of it can not be inherited, since it cannot be inherited so there is no way to call through the object of its subclass, but only through the class itself to invoke the object. It is therefore possible to say that private methods are bound together with classes that define this method.
Although the final method can be inherited, it cannot be overridden (overwritten), although the subclass object can be called, but it is called the final method defined in the parent class, (thus we can know to declare the method as the final type, one to prevent the method from being overwritten, The second is to effectively turn off dynamic binding in Java.
The constructor method is also not inherited (on the web there is also said that the subclass unconditionally inherits the parameterless constructor of the parent class as its own constructor, but the individual does not think it is appropriate because we know that the subclass is using super () to invoke the non-parametric constructor of the parent class to complete the initialization of the parent class. Instead of inheriting from the parent class, we should not say that the subclass inherits the constructor of the parent class, so at compile time you can also know which class the constructor method belongs to.
For the static method, I also say that the specific principle is not very clear. However, according to the information on the internet and my own experiments can be concluded that the static method can be inherited by the quilt class, but can not be overridden (overwrite), but the quilt class is hidden. (This means that if there is a static method in the parent class, if there is no corresponding method in its subclass, then the method in the parent class is used when the subclass object calls the method.) If the same method is defined in the child class, the method defined in the subclass is called. The only difference is that when a child class object is transformed to a parent, the object uses a static method in the parent class, regardless of whether the static method is defined in the subclass. Therefore, it is said that static methods can be hidden and cannot be overwritten. This is the same as the subclass that hides the member variables in the parent class. The difference between hiding and overriding is that when a subclass object is converted to a parent object, it is able to access the hidden variables and methods of the parent class, and cannot access the method that the parent class is overridden.
From the above we can conclude that if a method cannot be inherited or cannot be overwritten after inheritance, then this method takes a static binding.

Compilation and operation of Java
The Java compilation process is the process of compiling Java source files into bytecode (the JVM executable code, the. class file), in which Java does not deal with memory, and in this process the compiler parses the syntax, and if the syntax is incorrect, it will be an error.
The Java runtime refers to the JVM (Java Virtual machine) loading bytecode files and interpreting the execution. In this process is really the creation of memory layout, execute Java program.
There are two ways to execute Java bytecode: (1) Compile on the fly: The interpreter compiles the bytes into machine code before executing the machine code, (2) interprets the execution mode: The interpreter completes all operations of the Java bytecode program by interpreting and executing a small piece of code each time. (Here we can see that the Java program in the execution of the process is actually carried out two times, first turn into bytecode and then converted into machine code.) This is why Java can be compiled once and run everywhere. Install the corresponding Java virtual machine on different platforms, you can implement the same bytecode to the machine code on different platforms and run on different platforms.

As already mentioned in the Java method, in addition to the Final,static,private
and construction methods are pre-bound, all other methods are dynamically bound.
The typical occurrence of dynamic binding occurs under the transformation declarations of the parent and child classes:
For example: Parent p = new Children ();
Detailed details of the process are as follows:
1: The compiler examines the object's claim type and method name.
Assuming we call the X.F (args) method, and X has been declared as an object of Class C, the compiler enumerates all the methods in class C that have the name F and the F method inherited from the Class C superclass.
2: Next the compiler checks the parameter types provided in the method call.
If one of the parameter types in all methods with the name F is the most matched to the parameter type provided by the invocation, this method is called "overload resolution".

3: When the program runs and uses dynamic binding to invoke the method, the virtual machine must invoke a method version that matches the actual type of the object pointed to by X.

Suppose the actual type is D (subclass C), if the Class D defines f (string) Then the method is called, otherwise the method F (string) is searched in the superclass of D, and so on.

When a JAVA virtual machine calls a class method (a static method), it chooses the method that is called based on the type of the object reference, which is usually known at compile time. Conversely, when a virtual machine invokes an instance method, it chooses the method that is called based on the actual type of the object (which can only be known at run time), which is dynamic binding, which is polymorphic. Dynamic binding provides a great deal of flexibility to solve real business problems and is a very graceful mechanism.

Unlike methods, when you work with member variables (instance variables and class variables) in a Java class, you do not take a run-time binding, but a static binding in the general sense. So in the case of an upward transformation, the object's methods can find the subclass, and the object's properties (member variables) are also the properties of the parent class (The child class hides the parent class member variable).
Java code

  1. Public class Father {
  2. protected String name = "Father attribute";
  3. }
  4. Public class Son extends Father {
  5. protected String name = "Son attribute";
  6. public static void Main (string[] args) {
  7. Father sample = new Son ();
  8. System.out.println ("called Properties:" + sample.name);
  9. }
  10. }


conclusion, the calling member is the parent attribute.
This result indicates that the object of the subclass (referred to by the parent class handle) is called to the member variable of the parent class. Therefore, it must be clear that the runtime (dynamic) binding is only the object's approach to the category .
Now trying to call the member variable name of the subclass, what do I do? The simplest approach is to encapsulate the member variable into a method getter form .
The code is as follows:
Java code

  1. Public class Father {
  2. protected String name = "Father attribute";
  3. Public String GetName () {
  4. return name;
  5. }
  6. }
  7. Public class Son extends Father {
  8. protected String name = "Son attribute";
  9. Public String GetName () {
  10. return name;
  11. }
  12. public static void Main (string[] args) {
  13. Father sample = new Son ();
  14. System.out.println ("called Properties:" + sample.getname ());
  15. }
  16. }


Result: The property of the son is called
Java is a static binding method for attributes. This is because static bindings are a lot of benefits, and it allows us to find bugs in the program at compile time, not at runtime. This will improve the efficiency of the operation of the program! The dynamic binding of the method is to realize polymorphism, and polymorphism is a major feature of Java. Polymorphism is also one of the key object-oriented technologies, so it is worthwhile for Java to achieve polymorphism at the cost of efficiency.

Pre-Java (static) binding and late (dynamic) binding

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.