Java Dynamic binding

Source: Internet
Author: User

Before looking at data structure things often encounter: the list list = new LinkedList () such an upward transformation, the study of the upward transformation (just know how to use upward transformation, but do not know why the upward transformation, in my other blog introduced http:// Www.cnblogs.com/buptldf/p/4959480.html

It also saw the concept of dynamic binding, so I looked at it specifically.

Program binding: The procedure in which the method name is associated with the method entity when the method is called. There are static bindings and dynamic bindings for Java, as well as pre-and late-binding.

Static bindings:

It is bound at compile time and loaded into memory waiting to be called, implemented by compiler and its linked program, C is static binding. In Java, variables or attributes are statically bound, and all methods decorated by the Static,final,private method are statically bound.

Dynamic binding:

When the program is run, it is not processed at compile time, depending on the object binding. 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. That is, the compiler still does not know the type of the object at this point, 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. But at least we can think of it: they all have special types of information in the object. The process of dynamic binding: 1. A method table for the virtual machine to extract the actual type of the object; 2. Virtual machine search method signature; 3 Call method. Details View http://blog.csdn.net/sureyonder/article/details/5569617

In Java, almost all 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, for the static method and the final method because cannot be inherited, so at compile time can determine their values, they belong to the early bound. Specifically, the method and member variables of the private declaration cannot be inherited by the quilt class, and all private methods are implicitly specified as final (we can also know that the method is declared as one of the final types to prevent the method from being overwritten, and the second is to effectively close Dynamic binding in Java). Late binding in Java is implemented with the JVM, and we don't have to explicitly declare it.

Mainly used for upward transformation. For example: Father f = new Son ();

1. The compiler examines the object's declaration type and method name, and if called F.speak (args), the compiler will find all the methods of the Son class named method and the name speak method inherited from the Father class, to get a list of speak methods.

2. Then check the parameters of the Speak method, if object F finds the calling speak method and one of the methods found in the first step is the best match, the method in the method list is called the body (that is, what the method does), this process is called "overload resolution."

3: If the Found method list has two matching methods, one is the parent class, and one is a subclass rewrite, then the compiler will call the overridden method. If the subclass object calls a method, the method itself does not, it will go to its parent class to find, in fact, the second step in the call can only find a method that matches the calling method, that is, the method of the parent class.

More said unintentionally, see examples:

Java code


View Plaincopy to Clipboardprint?
public class father{
public void Method () {
System.out.println ("Parent class method, Object type:" +this.getclass ());
}
}

public class Son extends father{
public static void Main (string[] args) {
Father sample = NewSon ();//Upward transformation
Sample.method ();
}
}

A reference to the parent class is declared, but the object of the subclass is called during execution, and the program first looks for the method of the subclass object, but it is not found, so the parent class looks for

public class Son extends Father {


public void Method () {
System.out.println ("Subclass method, Object type:" +this.getclass ());
}

public static void Main (string[] args) {
Father sample = NewSon ();//Upward transformation
Sample.method ();
}
}

Because subclasses rewrite the method of the parent class, according to the above theory know will call the subclass method methods to execute, because the subclass object has method methods and not upward transformation to find

Java Dynamic binding

Related Article

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.