Static Binding and Dynamic Binding

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

提交回复
热议问题