java--Dynamic binding and polymorphism

Source: Internet
Author: User

Dynamic binding is the actual type of object that is judged during execution, rather than during compilation, and its corresponding method is called according to its actual type (determines which polymorphic form is called).

The first is that the parameter of the method is the parent class object, whether it is feasible to pass in the subclass object
then the parent P = new Children ();
This code is not very understanding, Google's process leads to the upward transformation
Understanding the upward transformation leads to dynamic binding
static bindings are also derived from dynamic bindings
 
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:
the method is already bound before the program executes, and is implemented by the compiler or other linker. For example: C.
for Java simple can be understood as the program compile time of the binding; In particular, the Java method is only final,static,private and the constructor method is pre-binding
 
Dynamic binding:
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. 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:
a method table that extracts the actual types of objects from a virtual machine;
Virtual machine Search method signature;
invokes a method.
 
a summary of binding Related:
after understanding the concepts of the three, it is clear that Java is late binding. 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. The second is to effectively close the dynamic bindings in Java. Late binding in Java is implemented with the JVM, and we don't have to explicitly declare it, and C + +, which must explicitly declare a method with late binding.
 
the upward transformation or polymorphism in Java is achieved through dynamic binding, so the dynamic binding is understood, and the upward transformation and polymorphism are done.
As we 've already said, for the methods in Java, except for the Final,static,private and constructor methods, all the 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 ();
Detailed details of the process are as follows:
1: The compiler examines the object's claim type and method name. Assuming we call the X.F (args) method, and X has been declared as an object of Class C, the compiler enumerates all the methods in class C that have the name F and the F method inherited from the Class C superclass .
2: Next the compiler checks the parameter types provided in the method call. If one of the parameter types in all methods with the name F is the most matched to the parameter type provided by the invocation, this method is called "Overload resolution" .
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. Suppose the actual type is D (subclass C), if the Class D defines f (string) Then the method is called, otherwise the method F (string) is searched in the superclass of D, and so on
 
above is the theory, here are a few examples (examples from the network):
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 = new Son ();//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
 
Java Code
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 = new Son ();//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
 
The previous theory has already mentioned the Java binding rules, so that when dealing with member variables in Java classes, it is not the runtime binding, but the general sense of static binding. So in the case of an upward transformation, the object's method can find the subclass, and the object's properties are also the properties of the parent class.
The code is as follows:
Java Code
Public class Father {
   
protected String name= "Father attribute";

Public Void Method () {
System.out.println ("Parent class method, Object type:" + this.getclass ());
}
}   

Public class Son extends Father {
protected String name= "son attribute";

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

Public static void Main (string[] args) {
Father sample = new Son ();//Upward Transformation
System.out.println ("called Member:" +sample.name);
}
}   
conclusion, the calling member is the parent attribute.
This result indicates that the object of the subclass (referred to by the parent class handle) is called to the member variable of the parent class. Therefore, it must be clear that the runtime (dynamic) binding is only the object's approach to the category.
now trying to call the member variable name of the subclass, what do I do? The simplest approach is to encapsulate the member variable into a method getter form.
The code is as follows:
Java Code
Public class Father {
protected String name = "Father Attribute";
Public String GetName () {
return name;
}
Public Void Method () {
System.out.println ("Parent class method, Object type:" + this.getclass ());
}
}   

Public class Son extends Father {
protected String name= "son attribute";

Public String GetName () {
return name;
}

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

Public static void Main (string[] args) {
Father sample = new Son ();//Upward Transformation
System.out.println ("called Member:" +sample.getname ());
}
}   
 
Result: The property of the son is called

 
 

java--Dynamic binding and polymorphism

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.