Static and Dynamic Polymophism
One feature in object-oriented programming languages which is essential to implementing polymorphism is dynamic bindning of functions. Procedural programming languages (like C, Pascal and COBOL etc.) offer static 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.
Static polymorphism refers to an entity existing in different physical forms simultaneously. Static polymorphism involves binding of functions on the basis of number, type and sequence, of their arguments. The various types of parameters are specified in the function declaration, and therefore the function can be bound to the class at compile tune. This form of associations is called binding. The term early binding terms from the fact when the program is executed, the calls are already bounds to the appropriate functions. The resolution is on the basis of number, type, arid sequence of argument declared for each form of the function. Consider the following function declarations:
void add (int, int);
void add (float, float);
Now, if the function add() is invoked, the parameters passed to it will determine which version of the function will be executed.This resolution is done at compile time
Dynamic (or late) binding is the concept of resolving a method call at run time. Dynamic polymorphism, refers to an entity changing its form depending on the circumstances. A function is said to exhibit dynamic polymorphism when it exists in more than one form, and calls to its various forms are resolved dynamically when the program is executed. Then term late binding refers to the resolution of the function to their associated methods at run-time instead of compile time. This feature increases the flexibility of the program by allowing the appropriate method to be invoked, depending on the content. The compiler is unable to bind a call to a method since resolution depends on the content of the call.
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.
Note that, the code on the sender side of the program will work even if we later on extend the system to handle new types of vehicles, because the sender expresses WHAT is to be done, instead of HOW. This is an example of polymorphism at work.