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 at runtime is called dynamic binding.
What happens in the code below:
Static binding or dynamic binding?
What kind of polymorphism does this show?
class Animal
{
void eat()
{
System.out.println("Animal is eating");
}
}
class Dog extends Animal
{
void eat()
{
System.out.println("Dog is eating");
}
}
public static void main(String args[])
{
Animal a=new Animal();
a.eat();
}
Case 1:
Case 2:
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
This really depends on overloading and overriding if you did something like this:
then in the main class:
the result in the two cases will be:
Animal is eating
but lets twist it some, lets have this:
then:
then in the main class:
now the result should be:
this is because overloading binds at compile time "static binding" while overriding binds at run time "dynamic binding"
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 isprivate
.final
implies the function cannot be changed and theprivate
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 runA
's method, regardless of whereA
references.check this employee class has abstract
earning()
function and each class has deferenttoString()
implementationAll calls to method
toString
andearnings
are resolved atexecution time
, based on thetype of the object
to which currentEmployee refers,This process is known as
dynamic binding
orlate binding
reference: Java™ How To Program (Early Objects), Tenth Edition
Your current code will output
Animal is eating
However, in your main class, if you created an object of type
Dog
and assigned it toAnimal
, then your output will beDog is eating
due to dynamic binding.Even though
a
is declared asAnimal
it is pointing to an object of typeDog
. So, at runtime, the object type is determined and appropriateeat()
method is called.One way to think of it is,
method overloading
is statically bound andmethod overriding
is dynamically bound.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:
Even if you change your
main
tothis will print
Animal is eating
, because the call tocallEat
uses static binding, and the compiler only knows thata
is of typeAnimal
.