Static Binding and Dynamic Binding

Static Binding and Dynamic Binding


Binding is a concept in which a method call is connected to the method body. In Java pr any other programming language there are two types of binding:

  1. Static binding
  2. Dynamic binding

We can also refer static binding as early binding and dynamic binding is referred as late binding.


Understanding type:


Variables have a type:

In programming languages all the variables have some type and this type may be primitive type or non primitive type. Consider the following example:

int id = 122;

In this line of code the variable id has an int data type or the variable is an integer type variable.


References have a type:

The references have also types for example this reference belongs to this class. Consider the following example in which it is cleared:

CODE:

class Cat {

public static void main (string args []) {

Cat c1;

}

}

In these lines of code the reference c1 is a type of class Cat.


Objects have a type:

In java programming language an object is considered as an instance of a particular class. This object is also considered as the instance of the super class.

Consider the following example:

CODE:

class Animal { }

class Cat extends Animal {

public static void main (string args []) {

Cat c1 = new Cat ();

}

}

In the above example the object c1 is an object and is also considered as the instance of the child class that is Cat and the instance of parent class that is Animal.


Static binding:

Static binding in java programming language exists when the type of the object is determined at the time of compilation by the compiler. Static binding in the program of java programming language exists when there is any private or static or final method in the class.


Example of static binding:

Consider the following example in which static binding is performed:

CODE:

class Cat {

private void display () {

system. out. println (“Cat is a mammal”);

}

public static void main (string args []) {

Cat c1 = new Cat ();

c1. display ();

}

}

In the above example the method of the class Cat is private and hence there is a static binding in our program.


Dynamic binding:

Dynamic binding in java programming language exists when the type of the object is determined at run time.


Example of Dynamic binding:

Consider the following example in which dynamic binding is performed:

CODE:

class Animal {

void display () {

system. out. println (“Animals are eating”);

}

}

class Cat extends Animal {

void display () {

system. out. println (“Cat is eating”);

}

public static void main (string args []) {

Animal a1 = new Cat ();

a1. display ();

}

}

OUTPUT:

Cat is eating

In the above example the compiler does not know the type of the object a. This is because the instance of the class Cat is also an instance of the class Animal. Therefore, compiler will only know the base type of the instance.

The type of the object will be decided at run time therefore we achieved dynamic binding.