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.
  • Example − Method 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.

packagecom.tutorialspoint;classAnimal{publicvoidmove(){System.out.println("Animals can move");}}classDogextendsAnimal{publicvoidmove(){System.out.println("Dogs can walk and run");}}publicclassTester{publicstaticvoidmain(String args[]){Animal a =newAnimal();// Animal reference and object// Dynamic Binding	  Animal b =newDog();// 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.

classAnimal{publicvoidmove(){System.out.println("Animals can move");}}classDogextendsAnimal{publicvoidmove(){super.move();// invokes the super class methodSystem.out.println("Dogs can walk and run");}}publicclassTestDog{publicstaticvoidmain(String args[]){Animal b =newDog();// Animal reference but Dog object
      b.move();// runs the method in Dog class}}

Output

Animals can move
Dogs can walk and run

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *