Java static binding and dynamic binding

Source: Internet
Author: User

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 (early bound compiler bindings):

The method is already bound before the program executes, and is implemented by the compiler or other linker. For example:C . For Java can be understood as the program compile-time binding; In particular, The method in Java is final, Static,private , and constructor methods are pre-bound

Dynamic bindings (late bound run-time bindings):

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. This means that the compiler still does not know the type of object at this time, 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. It can be said that they all want to have some special types of information in the object.

The process of dynamic binding:

Method table for virtual machine Fetch object's actual type

Virtual Machine Search Method signature

Calling methods

A summary of binding Related:

After understanding the concepts of the three, we foundJavabelongs to late binding. In theJava, almost all of the methods are late-bound, and the dynamic binding method is either a subclass or a base class at run time. But there are also special, forStaticMethods andFinalmethods cannot be inherited, so they can be determined at compile time, and they are pre-bound. A point of special note,Privatedeclared methods and member variables cannot inherit from a quilt class, allPrivatemethods are implicitly specified asFinalof the(This is what we know: Declaring a method asFinaltype of  one is to prevent the method from being overwritten, and the second is to effectively closeJavadynamic Binding in the). Javathe late binding in is determined by theJVMto implement, we don't have to explicitly declare it, andC + +is different,You must explicitly declare that a method has late binding. Javathe upward transformation or polymorphism is realized by dynamic binding, so understanding dynamic binding also makes the orientation transition and polymorphism.

For methods in Java , except 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 ();

The process is as follows :

1: The compiler examines the object's claim 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 lists C all methods in the class with the name F and the F Method inherited from the superclass of class C

2: Next the compiler examines the type of arguments provided in the method call. If all names are F  "

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. Assuming that the actual type is D ( subclass C ), if the class D defines f (String) The method is called, otherwise the method F (String) is searched in the superclass of D , and so on.

Problem thinking:

How to provide a method to the user a way to complete a task. What if the user has special requirements and can customize their own methods?

Knowledge Involved:

Child parent class, interface, upward transformation, dynamic binding

Specific code:

 Package Com.chengxuyuanzhilu;  Public Interface myinterfaces {    void  doting ();}
 Package Com.chengxuyuanzhilu;  Public class Implements myinterfaces {    @Override    publicvoid  doting () {        System.out.println ("I'm Drinking");}    }
 package   Com.chengxuyuanzhilu;  public  class  Eat implements   myinterfaces {@Override  Span style= "color: #0000ff;" >public  void   doting () {System.out.print    ln ( "I'm Eating" 
 Package Com.chengxuyuanzhilu;  Public class Implements myinterfaces {    @Override    publicvoid  doting () {        System.out.println ( "I'm Running");}    }
 PackageCom.chengxuyuanzhilu; Public classTestdynamicbind { Public Static voidMain (string[] args) {myinterfaces my=NULL; My=NewEat ();                Bind (my); My=NewDrink ();                Bind (my); My=NewRun ();                    Bind (my); }        Static voidbind (Myinterfaces my) {my.doting (); }}

Java static binding and 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.