Static Binding and Dynamic Binding

后端 未结 6 1504
灰色年华
灰色年华 2020-11-27 13:46

I am really confused about dynamic binding and static binding. I have read that determining the type of an object at compile time is called static binding and determining it

6条回答
  •  日久生厌
    2020-11-27 14:18

    check this employee class has abstract earning() function and each class has deferent toString() implementation

    Employee[] employees = new Employee[4];
    
    // initialize array with Employees
    employees[0] = new SalariedEmployee();
    employees[1] = new HourlyEmployee();
    employees[2] = new CommissionEmployee();
    employees[3] = new BasePlusCommissionEmployee();
    for (Employee currentEmployee : employees){
        System.out.println(currentEmployee); // invokes toString
        System.out.printf("earned $%,.2f%n", currentEmployee.earnings());
    }
    

    All calls to method toString and earnings are resolved at execution time, based on the type of the object to which currentEmployee refers,

    This process is known as dynamic binding or late binding

    reference: Java™ How To Program (Early Objects), Tenth Edition

提交回复
热议问题