Understanding static bindings and dynamic bindings in Java _java

Source: Internet
Author: User

The execution of a Java program is compiled and executed (interpreted) both steps, while Java is an object-oriented programming language. When the subclass and the parent class have the same method, the subclass overrides the method of the parent class, the program calls the method at run time whether it calls the parent class's method or the subclass's override method, which should be the problem we encountered in the beginner java. Here we will first determine which method implementation or variable is called binding.

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

The concept of program binding:

Binding refers to the invocation of a method associated with the class (method body) in which the method is located. For Java, bindings are divided into static and dynamic bindings, or early-bound and late-bound

static binding (early bound compiler Binding):

The method is already bound before the program executes, and is implemented by the compiler or other linker. For example: C. Java can be understood as the binding of the program compile-time; In particular, the Java method only Final,static,private and constructs the method is early bound

Dynamic binding (late-bound runtime bindings):

Late binding: Binding at run time 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 object at run time and invoke the appropriate method, respectively. This means that the compiler still does not know the type of object, but the method call mechanism can investigate itself, find the correct method body. Different languages differ in how they are implemented in late binding. It can be argued that they all want to have some special type of information in the object.

The process of dynamically binding:

    • Method table for virtual machines to extract the actual types of objects
    • Virtual Machine Search Method signature
    • Call method

About binding-related summaries:

After understanding the concept of the three, we found that Java belongs to late binding. In Java, almost all methods are late-bound, and the dynamic binding method at run time belongs to a subclass or a base class. But there are also special, for static methods and final methods cannot be inherited, so they can be determined at compile time, they belong to early binding. Specifically, private declarations of methods and member variables cannot be inherited by the quilt class, and all private methods are implicitly specified as final (thus we know that the method is declared as final type in order 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, and we don't have to explicitly declare it, and C + + is different and must explicitly declare that a method has late binding. Java in the upward transformation or polymorphism is the use of dynamic binding implementation, so understanding dynamic binding, also engage in directional transformation and polymorphism.

For methods in Java, except for the final,static,private and construction methods are early binding, all other methods are dynamically bound. The typical dynamic binding occurs under the transformation declaration of the parent class and subclass:

For example:Parent p = new Children ();

The specific process is as follows:

1, the compiler examines the object's declaring type and method name. Suppose we call the X.f (args) method and X has been declared as an object of Class C, then the compiler enumerates all the methods named F in Class C and F methods inherited from Superclass Class C.

2. The compiler then checks the parameter types provided in the method invocation. This method is called if there is a parameter type in all methods named F that best matches the type provided by the invocation, and this procedure is called "overload resolution".

3. When the program is run and the method is invoked using dynamic binding, the virtual machine must invoke a method version that matches the actual type of the object that X points to. Suppose the actual type is a subclass of D (c), and if the D class defines an F (string), the method is called, or the method F (string) is searched in the superclass of D, and so on.

Question thinking:

How to provide a method to the user of a method to complete a task. If users have special requirements and can customize their own methods?

Knowledge Involved:

Child parent class, interface, transition up, 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 ("I'm drinking Water") c9/>}

}



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 am Running");
  }

}



Package Com.chengxuyuanzhilu;

public class Testdynamicbind {public
  static void Main (string[] args) {
    myinterfaces i = 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 learn.

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.