Static and Dynamic Binding in Java

Introduction

In this article we discuss static and dynamic binding in Java.

What is binding

In simple words, linking or joining two things are called binding. In other words, simply joining two things or people.

In human beings, bindings are of various types, like emotional binding, financial binding, etcetera.

In terms of computers, connecting a method call to a method body is called binding.

It is of mainly two types:

  1. Static binding (in other words, called as early binding)
  2. Dynamic binding (in other words, called as late binding)

Their Type

1. Variable type

For example:

int d=150;

Here variable d is a type of int.

2. References have a type

class Cat
  {
    public static void main(String args[])
      {
Cat c1=new Cat();
      }
  }

Here c1 is an instance of the Cat class, but there is also an instance of Animal.

1. Static binding

It is defined as, when we compile our program and an object type is determined then it is known as static binding or early binding. If there is any method  in a class of final, private, or static type then it is static binding.

Example

class Cat
  {
    private void eating()
      {
        System.out.println("Cat is eating");
      }
    public static void main(String args[])
      {
        Cat c1=new Cat();
        c1.eating();
      }
  }

Output

Fig-1.jpg

Dynamic binding

When we run our program and an object type is determined then it is known as dynamic binding.

Example

class AnimalEx
  {
    public void eating()
      {
        System.out.println("Animal Start eating....");
      }
  }
class CatEx
  {
    public void eating()
      {
        System.out.println("Cat start eating....");
      }
    public static void main(String args[])
      {
        CatEx anml=new CatEx();
        anml.eating();
      }
  }

Output

Fig-2.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all