Static Binding and Dynamic Binding

Static Binding

When all the code is bound during compilation that binding is known Static Binding. When an object type is resolved at compile time, then it is called static binding.

Static binding is limited, and it may lead to difficulty in software maintenance. Static binding is formed private, final or static method in a class.

Example of Static Binding
		class CarDrive{
		   public static void Drive()
		   {
		       System.out.println("CarDrive by boy");
		   }
		}
		class Driving extends CarDrive{
		   public static void Drive(){
		       System.out.println("CarDrive by girl");
		   }
		   public static void main( String args[]) {

		       CarDrive obj = new Driving();


		       CarDrive obj2 = new CarDrive();
		       obj.Drive();
		       obj2.Drive();
		   }
		}
Output

CarDrive by boy

CarDrive by boy

In the above example, we have two classes CarDriving and Driving. Both the classes have same method CarDriving(), but the method is static, which means it cannot be overriden so even though I have used the object of Driving class while creating object obj, the parent class method is called by it because the reference is of CarDriving type (parent class). So whenever a binding of static, private and final methods happen, type of the class is determined by the compiler at compile-time and the binding happens.

Dynamic Binding

Dynamic Binding is an alternative approach to Static Binding. When an object type is resolved at run-time then it is called dynamic binding. The binding of variables to routines (or methods in object-oriented programming terms) is done at run time.

Example of Dynamic Binding
		class CarDrive{

		  public void Drive()
		   {
		       System.out.println("CarDrive by boy");
		   }
		}
		class Driving extends CarDrive{
		   public void Drive(){
		       System.out.println("CarDrive by girl");
		   }
		   public static void main( String args[]) {

		       CarDrive obj = new Driving();

		       CarDrive obj2 = new CarDrive();
		       obj.Drive();
		       obj2.Drive();
		   }
		}
Output

CarDrive by girl

carDrive by boy

This is the same example that we have seen above. The only difference here is that overriding is actually happening in this example since these methods are not static, private and final. In case of overriding the call to the overridden method is determined at runtime by the type of object thus late binding happens. The output is different than what we saw in the static binding example, because in this case while creation of object obj the type of the object is determined as a girl type, so the method of girl class is called. Remember the type of the object is determined at the runtime.

Difference between Static Binding vs Dynamic Binding

Now we discuss difference between Static Binding vs Dynamic Binding in Java.

  • Static binding is arrived at compile time whereas Dynamic binding is arrive at runtime.
  • The binding of variables to operations at compile time whereas the binding of variables to operations at run time.
  • The binding of overloaded methods is static whereas the binding of overridden methods is dynamic.
  • Binding of private, static and final methods always happens at compile time. When the method overriding is actually happening and the reference of parent type is assigned to the object of child class type then such binding is resolved during runtime.

Copyright 1999- Ducat Creative, All rights reserved.