Static and Dynamic Bindinding in Java


Binding:

Connecting a method call to the method body is known as binding.There are two types of binding.

  1. Static Binding (also known as early binding).
  2. Dynamic Binding (also known as late binding).

1. Static Binding:

It is also known as early binding.It is a compile time operation.When type of the object is determined at compiled time(by the compiler), it is known as static binding.

If there is any private, final or static method in a class, there is static binding.

Overloaded methods are bounded using static binding at compile time.

Static binding example:

class animal

{

//……….

}

class cat extends animal

{

public void walk()

{

System.out.println(“cat walks”);

}

public static void main( String args[])

{

cat obj1 = new cat();

obj1.walk();

}

Here we have created an object of cat class and calling the method walk() of the same class. Since nothing is ambiguous here, compiler would be able to resolve this binding during compile-time, such kind of binding is known as static binding.

Static binding with overloaded function in Java :

 

class abc

{

public void sum(int a)

{

System.out.println(“int value=”+a);

}

public void sum(float b)

{

System.out.println(“float value=”+b);

}

public void sum(String c)

{

System.out.println(“String value=”+c);

}

}

class xyz

{

public static void main(String arg[])

{

abc a1=new abc();

a1.sum(10);

a1.sum(2.4);

a1.sum(“Ram”);

}

}

output:

sum=10

sum=2.7

sum=Ram

Dynamic binding:

Dyanmic Binding is a RunTime operation.Overridden methods are bounded using dynamic binding at runtime.

Dynamic binding example

Here animal is a super class and cat is a child class since cat extends animal. Both these classes have a same method void walk(). Since we have assigned the parent class reference to the child class object, during call of walk() method the compiler would not be able to decide which walk() method to call. It will be confused between the walk() method of animal class and walk() method of cat class. Such kind of bindings are known as dynamic binding as compiler figures out the object type in runtime.

class animal

{

public void walk()

{

System.out.println(“animal walks”);

}

}

class cat extends anima

l{

public void walk()

System.out.println(“cat walks”);

}

public static void main( String args[])

{ 

//Reference is of parent class

animal obj = new cat();

obj.walk();

}

}

Output:

cat walks

Difference between Static and Dyanmic Binding in Java:

 

Difference between static and dynamic binding is a popular Java programming interview question which tries to explore candidates knowledge on have compiler and JVM finds which methods to call if there are more than one method of same name as it’s case in method overloading and overriding. This is also best way to understand what is static binding and what is dynamic binding in Java. In next section we will difference between both of them.

 

Static Binding Dynamic Binding
It is also known as early Binding. It is also known as Late Binding.
Static binding is a compile time operation. Dynamic binding is a runtime operation.
Static binding only uses Type information, and method resolution is based upon type of reference variable Dynamic or late binding resolves method based upon actual object.
In Java programming language, private, static and final method are resolved using static binding only virtual methods are resolved using dynamic binding.
Overloaded methods are bounded using static binding at compile time.e.g.

 

Class addition {

void sum(int a,int b){

System.out.println(a+b);}

void sum(int a,int b,int c){

System.out.println(a+b+c);}

public static void main(String args[]) {

addition obj=new addition();

obj.sum(10,10,10);  obj.sum(20,20);

}}

Overridden methods are bounded using dynamic binding.e.g

 

Class animal{

class Animal {

public void move(){

System.out.println(“Animals can move”);}}

class Dog extends Animal {

public void move() {

System.out.println(“Dogs can walk and run”);

}}

public class TestDog {

public static void main(String args[]) {

Animal a = new Animal(); // Animal reference and objectAnimal b = new Dog(); // Animal reference but Dog objecta.move();//output: Animals can move b.move();//output:Dogs can walk and run

}}

 

Comments are closed.