table of Contents
Dynamic binding of Java polymorphism
Part Review: polymorphic object-oriented programming is a very important feature, which allows the program to have a better readability and scalability.
- It occurred in the inheritance relationship.
- The method of the parent class subclasses require rewriting.
- Reference objects to sub-class type of the parent class type.
Throughout, polymorphic method for all , for the member variable in the class, there is no multi-state argument.
Part said: a base class reference variable to receive objects of different subclasses corresponding subclass will call, which is actually dynamic binding process. Before understanding the dynamic binding, to add some concepts.
Reference type variable
Reference type variable has two types: compile-time type and run-time type . (Also known statement type and the actual type) Here is a simple example:
//假设Student类是Person类的子类
Person p = new Student();
Compile-time type
- Also known as the type of statement, that by the time variable declaration determined type.
- The style
Person
is the reference variable p compile-time type.
Run-time type
- Also known as the actual type, that is, from point to the actual type of the object is determined.
- The style
Student
is the runtime reference type variable p.
Method binding
The method calls methods associated with the body is called binding .
Static binding
Prior to program execution bindings, called static binding, also known as early binding. In the process-oriented language is the default binding mode.
In Java, a private, static and final modification methods (summary will be made after static and final) or constructor can accurately make the compiler which method call is static binding (static binding).
Dynamic binding
At runtime according to the object run-time type of binding, called dynamic binding , also called late binding. Of course, in Java, in addition to those methods static binding, call the way other method is to dynamically bind it.
public class DynamicBinding {
//Object是所有类的超类,根据向上转型,该方法可以接受任何类型的对象
public static void test(Object x) {
System.out.println(x.toString());
}
public static void main(String[] args) {
test(new PrimaryStudent());//Student
test(new Student());//Student
test(new Person());//Person
test(new Object());//java.lang.Object@1b6d3586
}
}
class Person extends Object {
@Override
public String toString() {
return "Person";
}
public void run(){}
public void count(int a){}
}
class Student extends Person {
@Override
public String toString() {
return "Student";
}
public void jump(){}
}
class PrimaryStudent extends Student {
}
- Statement four invoke a method in the parameter, compile-time type is
Object
. Note: The reference variable can only be called compile-time type has a method. - When they run different types, the operation is interpreted at runtime, it calls the method of rewriting their respective types.
- The same type of reference variable, when calling the same method, exhibit different behavioral characteristics , which is the most intuitive multi-state manifestation of it.
Methods Table
We can also find test(new PrimaryStudent());
the operating results is Student
,, the results are obvious, because PrimaryStudent
the parent class's class did not rewrite, if the calling method dynamically binding, the virtual first find a suitable method in this class, if no, we would have been looking to the parent class, until you find.
So, each call to be looking up, spending a lot of time bound. Each class created this virtual machine in advance for the method table , which lists all the method signature (return type is not) and the actual method call can, this way, then, the direct look-up table when calling a method . (It is worth noting that, if the defined super call the parent method, the lookup table directly in the actual type of the parent class)
- The following is a
Person
class method table:
Person:
//下面省略Object方法签名
//xxx()-> Object.xxx()
//方法签名->实际调用的方法
toString()->Person.toString()
run()->Person.run()
count(int)->Person(int)
- The following is a
Student
class method table:
Student:
//下面省略Object方法签名
//xxx()-> Object.xxx()
//方法签名->实际调用的方法
toString()->Student.toString()
jump()->Student.jump()
run()->Person.run()
count(int)->Person(int)
- The following is a
PrimaryStudent
class method table (PrimaryStudent
class is empty, a direct inheritanceStudent
class):
PrimaryStudentt:
//下面省略Object方法签名
//xxx()-> Object.xxx()
//方法签名->实际调用的方法
toString()->Student.toString()
jump()->Student.jump()
run()->Person.run()
count(int)->Person(int)
- Therefore, the implementation of
test(new PrimaryStudent());
when the statement, the virtual machine will extractPrimaryStudent
the method table. - The virtual machine will search defined in Table
toString
signatures. Then you need to call a virtual machine already knowsStudent
the type oftoString()
method. - Finally, call the method is completed.
Dynamic binding greatly enhance the scalability of the program, for example, I now want to add a
Teacher
class, you can directlyTeache
r class inherits fromPerson
the class, and thenObject
refer to the class of pointTeacher
objects, rather than do other code tuning, dynamic binding Auto get, quite comfortably.
Reference books: "Thinking in Java", "Java core technology Volume I"