Java Tutorial

Java Control Statements

Object Oriented Programming

Java Built-in Classes

Java File Handling

Java Error & Exceptions

Java Multithreading

Java Synchronization

Java Networking

Java Collections

Java List Interface

Java Queue Interface

Java Map Interface

Java Set Interface

Java Data Structures

Java Collections Algorithms

Java Miscellaneous

Advanced Java

Java APIs & Frameworks

Java Useful Resources

Java - Dynamic Binding



Binding is a mechanism creating link between method call and method actual implementation. As per the polymorphism concept in Java, object can have many different forms. Object forms can be resolved at compile time and run time.

Java Dynamic Binding

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.

Characteristics of Java 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.

  • ExampleMethod overriding is the example of Dynamic binding.

  • Type of Methods − Virtual methods use dynamic binding.

Example of Java Dynamic Binding

In this example, we've created two classes Animal and Dog where Dog class extends Animal class. In main() method, we're using Animal class reference and assign it an object of Dog class to check Dynamic binding effect.

package com.tutorialspoint;

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
   }
}

Output

Animals can move
Dogs can walk and run

In the above example, you can see that even though b is a type of Animal it runs the move method in the Dog class. The reason for this is: In compile time, the check is made on the reference type. However, in the runtime, JVM figures out the object type and would run the method that belongs to that particular object.

Therefore, in the above example, the program will compile properly since Animal class has the method move. Then, at the runtime, it runs the method specific for that object.

Java Dynamic Binding: Using the super Keyword

When invoking a superclass version of an overridden method the super keyword is used so that we can utilize parent class method while using dynamic binding.

Example: Using the super Keyword

In this example, we've created two classes Animal and Dog where Dog class extends Animal class. Dog class overrides the move method of its super class Animal. But it calls parent move() method using super keyword so that both move methods are called when child method is called due to dynamic binding. In main() method, we're using Animal class reference and assign it an object of Dog class to check Dynamic binding effect.

class Animal {
   public void move() {
      System.out.println("Animals can move");
   }
}

class Dog extends Animal {
   public void move() {
      super.move();   // invokes the super class method
      System.out.println("Dogs can walk and run");
   }
}

public class TestDog {

   public static void main(String args[]) {
      Animal b = new Dog();   // Animal reference but Dog object
      b.move();   // runs the method in Dog class
   }
}

Output

Animals can move
Dogs can walk and run
Advertisements