Explanation of java Dynamic Binding and Static Binding Usage Examples

  • 2021-07-26 07:45:07
  • OfStack

This article illustrates the use of java dynamic binding and static binding. Share it for your reference, as follows:

Background

1. When the child class and the parent class have the same method, and the child class overrides the parent class's method, does the program call the parent class's method or the subclass's overridden method at runtime (especially in the case of upward type conversion)?

2. When there are methods with the same method name but different parameters (overloaded) in a class, how should the program distinguish which method to use when executing?

There is a binding mechanism in java to solve the above questions.

Binding

Binding: Associate the call of a method with the class (method body) where the method is located. That is, deciding which method and variable to call.

In java, binding is divided into static binding and dynamic binding. Also known as early binding and late binding.

Static binding

It is bound before the program executes (that is, it is known during compilation which class the method is in).

Among the methods in java, only the methods and constructions modified by final, static and private are statically bound.

private-decorated methods: private-decorated methods cannot be inherited, so the child class cannot access the private-decorated methods in the parent class. Therefore, the method body can only be called through the parent class object. Therefore, it can be said that the private method and the class that defines this method are bound in 1.

final modified method: It can be inherited by a subclass, but cannot be overridden (overridden) by a subclass, so what is actually called in the subclass is the final method defined in the parent class. (Two benefits of using final decorations: (1) Preventing methods from being overwritten; And (2) Turning off dynamic bindings in java).

static modified method: It can be inherited by subclasses, but can not be overridden (overridden) by subclasses, but can be hidden by subclasses. If there is one static method in the parent class, If there is no corresponding method in its subclass, When the subclass object calls this method, the method in the parent class will be used. And if the same method is defined in the subclass, The method defined in the subclass will be called. The only difference is that when the subclass object is uptyped to the parent class object, the object will use the static method in the parent class regardless of whether the static method is defined in the subclass, so it is said here that the static method can be hidden but not overwritten. This is the same as a subclass hiding a member variable in a parent class. The difference between hiding and overriding is that after a child class object is converted to a parent class object, it can access the hidden variables and methods of the parent class, but cannot access the overridden methods of the parent class.

Constructors: Constructors are also not inheritable (because the subclass calls the parent's constructor through the super method, or jvm automatically calls the default constructor of the parent class), so you can know which class the constructor method belongs to at compile time.

Therefore, if a method is inherited or cannot be overridden after being inherited, then the method adopts static binding

Dynamic binding

At run time, it binds according to the type of specific objects.

If a language implements late binding, it must also provide a mechanism to determine the type of object at run time and call the appropriate methods separately. That is, the compiler still doesn't know the type of the object at this time, but the method calling mechanism can investigate itself and find the correct method body. Different languages have different ways to implement late binding, but we can at least think of it as follows: They all need to insert some special type of information into objects.

The process of dynamic binding:

1. Method table for virtual machine to extract the actual type of object

2. Virtual machine search method signature

3. Call the method

In java, overloaded methods use static binding, and overridden methods use dynamic binding.

Experiment


package practice;
public class Bind{
 public static void main(String[] args) {
 Child c = new Child();
 Parent p = c;
 
 System.out.println(p.getPristr());
 System.out.println(c.pristr);
 c.print();
 p.print();
 
 c.print1();
 p.print1();
 
 c.print2();
 p.print2();
 
 }
}
class Parent{
 private String pristr = "parent private string";
 String pubstr = "public string";
 
 public String getPristr() {
 return pristr;
 }
 public void setPristr(String pristr) {
 this.pristr = pristr;
 }
 public Parent() {
 System.out.println("parent Constructor ");
 }
 
  final public void print() {
   System.out.println("parent Adj. print");
  }
  public static void print1() {
   System.out.println("parent Adj. print1");
  }
  public void print2() {
   System.out.println("parent Adj. print2");
  }
}
class Child extends Parent{
 String pristr = "child private string";
 String pubstr = "public string";
 
 public Child() {
 System.out.println("child Constructor ");
 }
 
 public static void print1() {
 System.out.println("child Adj. print1");
  }
 
 public void print2() {
   System.out.println("child Adj. print2");
  }
}

Results

parent constructor
child constructor
parent private string
child private string
print for parent
print of parent
print1 of child
print1 of parent
print2 of child
print2 of child

More readers interested in java can check the topics of this site: "Introduction and Advanced Tutorial of Java Object-Oriented Programming", "Tutorial of Java Data Structure and Algorithm", "Summary of Java Node Operation Skills", "Summary of Java File and Directory Operation Skills" and "Summary of Java Cache Operation Skills"

I hope this article is helpful to everyone's java programming.


Related articles: