Static Binding and Dynamic Binding

后端 未结 6 1448
灰色年华
灰色年华 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:02

    Your example is dynamic binding, because at run time it is determined what the type of a is, and the appropriate method is called.

    Now assume you have the following two methods as well:

    public static void callEat(Animal animal) {
        System.out.println("Animal is eating");
    }
    public static void callEat(Dog dog) {
        System.out.println("Dog is eating");
    }
    

    Even if you change your main to

    public static void main(String args[])
    {
        Animal a = new Dog();
        callEat(a);
    }
    

    this will print Animal is eating, because the call to callEat uses static binding, and the compiler only knows that a is of type Animal.

    0 讨论(0)
  • 2020-11-27 14:05

    Your current code will output Animal is eating

    However, in your main class, if you created an object of type Dog and assigned it to Animal, then your output will be Dog is eating due to dynamic binding.

    public static void main(String args[])
    {
        Animal a = new Dog(); // An object of Dog is assigned to Animal
        a.eat(); // Dynamically determines which eat() method to call
    }
    

    Even though a is declared as Animal it is pointing to an object of type Dog. So, at runtime, the object type is determined and appropriate eat() method is called.

    One way to think of it is, method overloading is statically bound and method overriding is dynamically bound.

    0 讨论(0)
  • 2020-11-27 14:17

    For non-static functions, you use static binding whenever the function is non-virtual, i.e. the final keyword is applied to it and/or the function is private. final implies the function cannot be changed and the private keyword implies it only has class scope. Otherwise, dynamic binding is used.

    For static functions, static binding is always used. If a type A is passed in, it will run A's method, regardless of where A references.

    0 讨论(0)
  • 2020-11-27 14:17

    Case 1:

    Animal a =new Animal();
    a.eat();
    

    Case 2:

    Animal a=new Dog(); 
    a.eat();
    

    Here both is dynamic bind because during compile time the type of the object is determined but at runtime based on the instance the object that is assigned the corresponding eat method would be bind dynamically by the JVM .

    In the first case the animal class eat method is called whereas in the second the dog class eat is called as the Animal object is assigned an Dog instance.The instance of Dog is also an instance of animal. That is you can take it as "is a" relation a dog is a animal.So here the type of object is determined as dog at runtime and JVM dynamically binds the eat method of the dog class.

    Check this links too

    http://www.javatpoint.com/static-binding-and-dynamic-binding

    http://www.coderanch.com/t/386124/java/java/Static-Binding-Dynamic-Binding

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-11-27 14:19

    This really depends on overloading and overriding if you did something like this:

    public class Animal{}
    
    
    public class Dog extends Animal{}
    
    public class AnimalActivity{
    
        public void eat(Animal a){
            System.out.println("Animal is eating");
        }
    
        public void eat(Dog d){
            System.out.println("Dog is eating");
        }
    }
    

    then in the main class:

    public static void main(String args[])
    {
        Animal a=new Animal();
        Animal d=new Dog();
        AnimalActivity aa=new AnimalActivity();
        aa.eat(a);
        aa.eat(d);
    }
    

    the result in the two cases will be: Animal is eating

    but lets twist it some, lets have this:

    public class Animal{
        public void eat(){
            System.out.println("Animal is eating");
        }
    }
    

    then:

    public class Dog extends Animal{
        public void eat(){
            System.out.println("Dog is eating");
        }
    }
    

    then in the main class:

    public static void main(String args[]){
        Animal d=new Dog();
        Animal a=new Animal();
        a.eat();
        d.eat();
    }
    

    now the result should be:

    Animal is eating
    Dog is eating
    

    this is because overloading binds at compile time "static binding" while overriding binds at run time "dynamic binding"

    0 讨论(0)
提交回复
热议问题