Dynamic Binding

 In dynamic binding, the type of object is determined at run-time i.e., before code's final run and before compilation.

Let's understand the need of dynamic Binding:
       Suppose we have a Vehicle having multiple vehicles like car, bike, bus etc. Now if we need to implement a Vehicle class in Java, then for each type of vehicle we have to create rebuild() method for each and every vehicle by providing their vehicle type as a parameter. 
      Now if there are 1000s of Vehicles then for each vehicle, we have to declare the same set of methods again and again.
     Example: 
            class Vehicle{
                    public void rebuild(Bike bike){
                         //Statements
                    }

                    public void rebuild(Car car){
                        //Statements
                    }
            }

 Hence to overcome this situation, we use Dynamic Binding.

     Example: 
           interface Vehicle{
                     int noOfWheels;
                     //statements
           }
  
           class Car implements Vehicle{
                     //Statements
           }

           class Bike implements Vehicle{
                     //Statements
           }

           class RebuildVehicle{
                  public static void rebuild(Vehicle vehicle){
                    //Statements
              }
           } 

           public class Program{
                  public static void main(String args[]){
                     Car car = new Car();
                     Bike bike = new Bike();
                     RebuildVehicle.rebuild(car);
                     RebuildVehicle.rebuild(bike);

                     //We can also create an instance using dynamic Binding 
                     Vehicle v1 = new Bike();
            }
           }

More examples on dynamic Binding..

          class Vehicle{
                  int noOfWheels;
                  String color;
        
                 public void run(int maxSpeed){
                        System.out.println("Vehicle of color "+this.color+" is running at Max Speed: "+ maxSpeed);
                 }
          }

          class Bike extends Vehicle{ 
                    public Bike(int noOfWheels, String color){
                             this.noOfWheels = noOfWheels;
                             this.color = color;
                     }

                    public void changeColor(String color){
                              this.color = color;
                    }
          }


          public class Program{
               public static void main(String args[]){

                    //Create object of bike using dynamic Binding
                   Vehicle v= new Bike(2, "red");
                   v.run(180); // Output: Vehicle of color red is running at Max Speed: 180

                   v.changeColor("blue"); 
                   // this line of code will throw an error, this is because in parent class Vehicle there is no method named changeColor() available. 

               //Hence to access this method you need to declare the object without applying dynamic binding
                 Bike bike = new Bike(2, "blue");
                 bike.run(180);
                 bike.changeColor("red");
            }
          }

Continue Learning: Functional Interface




Comments

Popular Posts