Static Binding and Dynamic Binding
Association of method definition to the method call is known as binding.
There are two types of Binding
- static binding or early binding
- dynamic binding or late binding
Static Binding
The binding which can be resolved at compile-time by the compiler is known as static binding or early binding.
If there is any private, final, and static method in class, there is static binding, because all the private, final, static method have always been bonded at compile time.
Why private, final and static methods is always a static binding ?
Private, final and static method is always a static binding because compiler knows that all private, final and static methods cannot be overridden and will always be accessed by object of local class. Hence compiler does not have any problem to determine object of a class(Local class ), that is the reason binding for such a method is static.
Static binding examples
For example 1:
class Vehicle
{
void show()
{
System.out.println("vehicle");
}
}
class Bike extends Vehicle
{
void run()
{
System.out.println("Bike run fast");
}
public static void main(String args[])
{
Bike b = new Bike();
b.run();
}
}
output: Bike run fast
In the above example, we have created an object of Bike class and calling the method of the same class i.e Bike class. Since nothing is ambiguous that means there is not two method with same name, compiler will easily resolve this binding during compile time, such kind of binding is known as static binding.
For example 2:
If there is private, final and static method in a class , also a example of static binding.
class Dog
{
private void run()
{
System.out.println("Dog is running");
}
public static void main(String args[])
{
Dog d = new Dog();
d.run();
}
}
output: Dog is running
In the above example, we have created an object of Bike class and calling the method of the same class i.e Bike class. Since nothing is ambiguous that means there is not two method with same name, compiler will easily resolve this binding during compile time, such kind of binding is known as static binding.
For example 2:
If there is private, final and static method in a class , also a example of static binding.
class Dog
{
private void run()
{
System.out.println("Dog is running");
}
public static void main(String args[])
{
Dog d = new Dog();
d.run();
}
}
output: Dog is running
Dynamic Binding
Dynamic binding is also called late binding. In case of dynamic binding compiler is not able to resolve the call or binding at compile time, When type of object is determine at run-time, it is known as dynamic binding.
The best example of dynamic or late binding is method overriding because in overriding parent and child class both have same name method. Thus when we calling the overridden method , the compiler get confused between parent and child class method because both have same method.
For example:
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[])
public static void main(String args[])
{
Animal a = new Dog();
a.eat();
}
}
ouptut: Dog is eating
ouptut: Dog is eating
In the above example, Animal and Dog class both have same method eat() so there compiler would not be able to decide which eat() method to call because here we have assigned the parent class reference to child class object. So such kind of bindings are known as dynamic binding because compiler check the object type at run time.
Difference between Static Binding and Dynamic Binding
- Dynamic binding is happens at run-time while static binding is the example of compile time.
- If there is private, final and static methods in a class, always happens at compile time because we cannot override these methods. Binding of overridden methods happens at run-time.
- Overloaded method(Overloading) is the example of static binding and overridden method(Overriding) is the example of dynamic binding
0 comments:
Post a Comment