what is static or early binding and dynamic or late binding?
what is static or early binding and dynamic or late binding?
Static (or early) binding is the concept of resolving a function/method call at compile time. Function/subroutine calls are bound to specific computer memory locations where functions/subroutines are placed at compile time.
Dynamic (or late) binding is the concept of resolving a method call at run time. The information available to the compiler at compile time, is the name of an operation, and a branch of the class hierarchy where this operation is defined:
myVehicle -> stop;
At run-time, the correct version of the stop method is selected, i.e. the version which corresponds to the class of the object which the variable myVehicle currently refers to, e.g. stop for Cars, stop for Plane etc
A code example of Static vs. Dynamic Bindning
Static Binding
The code segment below checks the type of the current Vehicle and calls the corresponding function:
switch (myVehicle->type) {
case CAR:
x = stopCar();
break;
case PLANE:
x = stopPlane();
break;
case BIKE:
x = stopBike();
break;
default:
error("Invalid");
}
This is difficult and error-prone to maintain, when new types of vehicles are added...
Dynamic Binding
The switch-statement above can be replaced by sending a message to the object which the variable myVehicle currently refers to:
x = myVehicle->stop();
In C++, the variable myVehicle would have been declared to be of type "reference to an instance of class Vehicle, or one of its subclasses":
Vehicle* myVehicle;
If you want more detailed example then visit the following page..
http://msmvps.com/blogs/rakeshrajan/.../01/45188.aspx
-----------------------
suresh