Tags

, ,

early binding is when the compiler knows at compile time what type to bind a variable to, where as late binding is when the binding of the variable is decided at run time.

Example: function overriding is late binding whereas function overloading is early binding. Similarly reflection based method invocation in Java is late binding. In JavaScript functions ‘this’ is late binding.

Class A 
{
 public void f(int x)
 {
 ...
 }
 
 public void g()
 {
 ...
 }
}

Class B extends A 
{
 public void f(int x, int y)
 {
 ...
 }

 public void g()
 {
 ...
 }

}

public static void testBinding(A obj)
{
 obj.f(10); // calls A::f() , early binding
 obj.f(10, 20) // calls B::f(), early binding
 obj.g(); // late binding, cannot determine at compile time whether to call A::g() or B::g()
}
Advertisements