Home  >  Article  >  Java  >  Understand static binding and dynamic binding in Java

Understand static binding and dynamic binding in Java

高洛峰
高洛峰Original
2016-12-26 16:07:521296browse

The execution of a Java program requires two steps: compilation and execution (interpretation). At the same time, Java is an object-oriented programming language. When the subclass and the parent class have the same method, and the subclass overrides the method of the parent class, when the program calls the method at runtime, should it call the method of the parent class or the overridden method of the subclass? This should be the question when we first learn Java. problems encountered. Here first we will determine which method to call or the operation of variables is called binding.

There are two binding methods in Java, one is static binding, also called early binding. The other 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 (method body) in which the method is located. For Java, binding is divided into static binding and dynamic binding; or early binding and late binding

Static binding (early binding compiler binding):

The method has been bound before the program is executed, which is implemented by the compiler or other linker. For example: C. For Java, it can be understood as binding during program compilation; in particular, the only methods in Java are final, static, private and constructor methods, which are early binding

dynamic binding (late binding runtime binding) ):

Late binding: Binding is performed at runtime based on the type of the specific object.

If a language implements late binding, it must also provide some mechanism to determine the type of the object during runtime and call the appropriate methods respectively. That is to say, the compiler still does not know the type of the object at this time, but the method calling mechanism can investigate by itself and find the correct method body. Different languages ​​implement late binding differently. Think of it this way: They all have to insert some special type of information into the object.

The process of dynamic binding:

The virtual machine extracts the method table of the actual type of the object

The virtual machine searches for the method signature

Calls the method

Summary on binding:

After understanding the concepts of the three, we found that java belongs to late binding. In Java, almost all methods are late bound. Dynamically bound methods belong to subclasses or base classes at runtime. But there are also special ones. Since static methods and final methods cannot be inherited, their values ​​can be determined at compile time. They are early binding. A special point to note is that methods and member variables declared privately cannot be inherited by subclasses. All private methods are implicitly designated as final (from this we know: declaring methods as final type is to prevent the method from being overwritten. , the second is to effectively turn off dynamic binding in java). Late binding in Java is implemented by the JVM. We do not need to declare it explicitly, but C++ is different. We must explicitly declare that a method has late binding. Upcasting or polymorphism in Java is achieved with the help of dynamic binding, so understanding dynamic binding means upcasting and polymorphism.

For methods in Java, except for final, static, private and constructor methods, which are pre-bound, all other methods are dynamically bound. Dynamic binding typically occurs under the conversion statement between the parent class and the subclass:

For example: Parent p = new Children();

The specific process is as follows:

1. The compiler checks the declared type and method name of the object. Suppose we call the x.f(args) method, and x has been declared as an object of class C, then the compiler will enumerate all methods named f in class C and the f methods inherited from the super class of class C

2. Next, the compiler checks the parameter types provided in the method call. If among all the methods named f, there is a parameter type that best matches the parameter type provided by the call, then this method is called. This process is called "overload resolution"

3. When the program is running and uses dynamic When a binding calls a method, the virtual machine must call a version of the method that matches the actual type of the object pointed to by x. Assume that the actual type is D (a subclass of C). If class D defines f(String), then this method is called. Otherwise, the method f(String) is searched for in the superclass of D, and so on

Problem thinking:

How to provide method users with a method to complete a task. What if the user has special requirements and can he customize his own method?

Involved knowledge:

Child and parent classes, interfaces, 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("我在喝水");
  }
 
}
 
 
 
package com.chengxuyuanzhilu;
 
public class Eat implements MyInterfaces {
 
  @Override
  public void doting() {
    System.out.println("我在吃东西");
  }
 
}
 
 
 
package com.chengxuyuanzhilu;
 
public class Run implements MyInterfaces {
 
  @Override
  public void doting() {
    System.out.println("我在奔跑");
  }
 
}
 
 
 
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 the entire content of this article, I hope it will be helpful to everyone's study.

For more articles related to understanding static binding and dynamic binding in Java, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn