Difference between object oriented object based programming language

What is Dynamic Binding ?

Answer : Dynamic binding or late binding is a mechanism in java in which the method being called upon and objects is looked up by name at the runtime.
In Dynamic or late binding the compiler do not now about its method and information to verify its existence, only compiler will see for any compilation issue, instead the method will be looked up by name at runtime.

In java method overriding forms is one of Java’s most powerful concepts and dynamic method dispatch which are also called late binding. It is the technique through which a call will be made to overridden method that will be resolved at run time, rather than compile time. It is important because this is how Java implements run-time polymorphism. Please see example below:

package com.javahonk.dynamicbinding;

public class DynamicBinding {

    public static void main(String[] args) {
	ABC a = new ABC(); // object of type ABC
	DEF b = new DEF(); // object of type DEF
	IJK c = new IJK(); // object of type IJK
	ABC r; // obtain a reference of type ABC

	r = a; // r refers to an ABC object
	r.callmeTest(); // calls ABC's version of callme
	r = b; // r refers to a DEF object
	r.callmeTest(); // calls DEF's version of callme
	r = c; // r refers to a IJK object
	r.callmeTest(); // calls IJK's version of callme

    }

}

// Dynamic Method Dispatch
class ABC {
    void callmeTest() {
	System.out.println("Inside ABC's callme method");
    }
}

class DEF extends ABC {
    // override callme()
    void callmeTest() {
	System.out.println("Inside DEF's callme method");
    }
}

class IJK extends DEF {
    // override callme()
    void callmeTest() {
	System.out.println("Inside IJK's callme method");
    }
}

 

Output:

What is Dynamic Binding

 

Leave a Reply

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