Binding In OOPS

Illusory Figure Of Java Binding

  • In OOPs Binding is a process to connect method call to the method body.
  • They are two types of Static(Early) and Dynamic(Late) Binding.
  • In Static Binding object is determined at compiled time. This is done using static, private and, final methods which can not be override.
  • In Dynamic Binding object is determined at run time.This is done using instance methods.

Static Binding:

public class Animal {
   public void sound() {
      System.out.println("Animal is speaking!");
   }
}

public class Dog extends Animal {
   public void sound() {
      	System.out.println("Dog is barking!");
   }
	
}

public class Main {
   public static void main(String[] args) {
      Animal animal= new Animal();//Static binding
      animal.sound();
      Animal dog= new Dog();//Dynamic binding
      dog.sound();
   }
}

Output: 

Animal is speaking!
Dog is barking!

Dynamic Binding:

public class Animal {
   public static void sound() {
      System.out.println("Animal is speaking!");
   }
}

public class Dog extends Animal {
	public static void sound() {
      System.out.println("Dog is barking!");
   }
	
}

public class Main {
   public static void main(String[] args) {
      Animal animal= new Animal();//Static binding
      animal.sound();
      Animal dog= new Dog();//Dynamic binding
      dog.sound();
      Dog.sound();
      Animal.sound();
   }
}

Output: 

Animal is speaking!
Animal is speaking!
Dog is barking!
Animal is speaking!

Scroll to Top