Dynamic binding of Java polymorphism

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 Personis 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 Studentis 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 PrimaryStudentthe 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 Personclass method table:
Person:
    //下面省略Object方法签名
    //xxx()-> Object.xxx()
    //方法签名->实际调用的方法
    toString()->Person.toString()
    run()->Person.run()
    count(int)->Person(int)
  • The following is a Studentclass method table:
Student:
    //下面省略Object方法签名
    //xxx()-> Object.xxx()
    //方法签名->实际调用的方法
    toString()->Student.toString()
    jump()->Student.jump()
    run()->Person.run()
    count(int)->Person(int)
  • The following is a PrimaryStudentclass method table ( PrimaryStudentclass is empty, a direct inheritance Studentclass):
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 extract PrimaryStudentthe method table.
  • The virtual machine will search defined in Table toStringsignatures. Then you need to call a virtual machine already knows Studentthe type of toString()method.
  • Finally, call the method is completed.

Dynamic binding greatly enhance the scalability of the program, for example, I now want to add a Teacherclass, you can directly Teacher class inherits from Personthe class, and then Objectrefer to the class of point Teacherobjects, rather than do other code tuning, dynamic binding Auto get, quite comfortably.

Reference books: "Thinking in Java", "Java core technology Volume I"

Guess you like

Origin www.cnblogs.com/summerday152/p/12049565.html