Understand static and dynamic binding in Java

  • 2020-05-05 11:18:39
  • OfStack

An Java program is compiled and interpreted, and Java is an object-oriented programming language. When a subclass and a parent class have the same method, and the subclass overrides the method of the parent class, does the program call the method at run time to call the method of the parent class or the subclass overrides the method? This should be the problem we encountered when we first learned Java. The first thing we'll do here is determine which method implementation or variable is called a binding.

There are two kinds of binding in Java, one is static binding, also known as early binding. The other is dynamic binding, also known as late binding.

The concept of program binding:

A binding is an invocation of a method that is associated with the class (the method body) in which the method resides. For java, there are static binding and dynamic binding. Or early binding and late binding

static binding (early binding compiler binding) :

The method is bound before the program executes and is implemented by a compiler or other linker. For example: C. For java it can be understood as a binding at compile time; In particular, the only methods in java are final, static, private and the constructors are pre-bound to

dynamic binding (late binding runtime binding) :

Late binding: binding at run time according to the type of specific object.

If a language implements late binding, mechanisms must be provided to determine the type of object at run time and to invoke the appropriate methods separately. That is, the compiler still doesn't know the type of object, but the method invocation mechanism can investigate on its own to find the right method body. Different languages implement late binding differently. Think of it this way: they all want to embed some special type of information in the object.

Dynamic binding process:

The virtual machine extracts the actual type of object from the method table virtual machine search method signature calls the method

summary of bindings:

After understanding the concepts of the three, we found that java belongs to late binding. In java, almost all methods are late-bound, and dynamically bound methods at run time belong to subclasses or base classes. In particular, the static and final methods are not inherited, so their values can be determined at compile time. In particular, the methods and member variables declared by private cannot be subclassed, and all private methods are implicitly specified as final (thus we know that declaring methods as   of type final prevents methods from being overwritten and effectively closes dynamic binding in java). Late binding in java is implemented by JVM, so we don't have to explicitly declare it. Unlike C++, we must explicitly declare that a method has late binding. The upward transformation or polymorphism in java is realized by means of dynamic binding, so understanding dynamic binding can solve the upward transformation and polymorphism.

For methods in java, all methods are dynamically bound except final, static, private and the constructor are early-stage bindings. The typical dynamic binding occurs under the parent and child conversion declarations:

For example: Parent p = new Children();

Here's how:

1. The compiler checks the declaration type and method name of the object. If we call the x.f (args) method, and x has been declared as an object of the C class, the compiler lists all the methods in the C class named f and the f method

inherited from the superclass of the C class

Next, the compiler checks the parameter types provided in the method call. If there is an argument type in a method named f that best matches the argument type provided by the call, the method is called, and the procedure is called "overload resolution"

3. When a program runs and calls a method using dynamic binding, the virtual machine must call the version of the method that matches the actual type of the object pointed to by x. Assuming the actual type is D(a subclass of C), the method is called if the D class defines f(String), otherwise it searches for the method f(String) in D's superclass, and so on

problem thinking:

How to provide a method to a method user to complete a task. If the user has special requirements and can customize their own method?

involves knowledge:

Child parent class, interface, transition up, dynamic binding

:


package com.chengxuyuanzhilu;

public interface MyInterfaces {
  void doting();
}



package com.chengxuyuanzhilu;

public class Drink implements MyInterfaces {

  @Override
  public void doting() {
    System.out.println(" I was 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 the entire content of this article, I hope to help you with your study.


Related articles: