DEV Community

Divya Divya
Divya Divya

Posted on

Dynamic Binding in Java?

  • Dynamic binding refers to the process in which linking between method call and method implementation is resolved at run time (or, a process of calling an overridden method at run time).
  • Dynamic binding is also known as run-time polymorphism or late binding. Dynamic binding uses objects to resolve binding.

Characteristic of Dynamic binding:

Linking − Linking between method call and method implementation is resolved at run time.

Resolve mechanism − Dynamic binding uses object type to resolve binding.

Example − Method overriding is the example of Dynamic binding.

Type of Methods − Virtual methods use dynamic binding.
package com.tutorialspoint;

Example


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 Tester {

   public static void main(String args[]) {
      Animal a = new Animal();   // Animal reference and object
      // Dynamic Binding      
      Animal b = new Dog();   // Animal reference but Dog object

      a.move();   // runs the method in Animal class
      b.move();   // runs the method in Dog class
   }
}

Enter fullscreen mode Exit fullscreen mode

Top comments (0)