20 Apr 2017

Difference Between Static and Dynamic Binding


BindingAssociation of a ‘function definition’ to a ‘function call’ or an association of a ‘value’ to a ‘variable’, is called ‘binding’. During compilation, every ‘function definition’ is given a memory address; as soon as function calling is done, control of program execution moves to that memory address and get the function code stored at that location executed, this is binding of ‘function call’  to ‘function definition’. Binding can be classified as ‘static binding’ and ‘dynamic binding’. If it’s already known before runtime, which function will be invoked or what value is allotted to a variable, then it is a ‘static binding’, and if it comes to know at the run time then it is called ‘dynamic binding’.

Content: Static Vs Dynamic Binding

  1. Comparison Chart
  2. Definition
  3. Key Differences
  4. Conclusion

Comparison chart:

BASIS FOR COMPARISONSTATIC BINDINGDYNAMIC BINDING
Event OccurrenceEvents occur at compile time are "Static Binding".Events occur at run time are "Dynamic Binding".
InformationAll information needed to call a function is known at compile time.All information need to call a function come to know at run time.
AdvantageEfficiency.Flexibility.
TimeFast execution.Slow execution.
Alternate nameEarly Binding.Late Binding.
Exampleoverloaded function call, overloaded operators.Virtual function in C++, overridden methods in java.

Definitions of Static Binding

When compiler acknowledges all the information required to call a function or all the values of the variables during compile time, it is called “static binding”. As all the required information are known before runtime, it increases the program efficiency and it also enhances the speed of execution of a program.
Static Binding makes a program very efficient, but it declines the program flexibility, as ‘values of variable’ and ‘function calling’ are predefined in the program. Static binding is implemented in a program at the time of coding.
Overloading a function or an operator are the example of compile time polymorphism i.e. static binding.

Implementation of static binding in C++ with example of overloading

  1. class overload{
  2. int a, b;
  3. public:
  4. int load(int x){ // first load() function.
  5. a=x;
  6. return a;
  7. }
  8. int load(int x, int y){ //second load() function.
  9. a=x;
  10. b=y;
  11. return a*b;
  12. }
  13. };
  14. int main(){
  15. overload O1;
  16. O1.load(20); //This statement binds the calling of function to 'first' load() function.
  17. O1.load(20,40); //This statement binds the calling of function 'second' load() function.
  18. }
Now, here  the statement ‘O1.loads(20)’ clearly binds the function calling to first ‘load(int x)’ function as it is the only function which accepts single integer argument. The statement O1.loads(20,40) binds the function calling to second ‘load(int x, int y)’ function as it is the only function which accepts two integer arguments. Hence, no time will be wasted in deciding which function to invoke; this will make program execution efficient and fast.

Definition of Dynamic Binding

Calling a function or assigning a value to a variable, at run-time is called “Dynamic Binding”. Dynamic binding can be associated with run time ‘polymorphism’ and ‘inheritance’ in OOP. Dynamic binding makes the execution of program flexible as it can be decided, what value should be assigned to the variable and which function should be called, at the time of program execution. But as this information is provided at run time it makes the execution slower as compared to static binding.

Implementation of dynamic binding using ‘virtual functions’ in C++.

  1. class base{
  2. public:
  3. virtual void funct(){ // Virtual function.
  4. cout<<"This is a base class's funct()";
  5. }
  6. };
  7. class derived1 : public base{
  8. public:
  9. void funct(){ //overridden virtual function.
  10. cout<<"This is a derived1 class's funct()";
  11. }
  12. };
  13. class derived2 : public base{
  14. public:
  15. void funct(){ //overridden virtual function.
  16. cout<<"This is a derived2 class's funct()";
  17. }
  18. };
  19. int main()
  20. {
  21. base *p, b;
  22. derived1 d1;
  23. derived2 d2;
  24. *p=&b;
  25. p->funct(); //The above statement decides which class's function is to be invoked.
  26. *p=&d1; // Vlaue of the pointer changes.
  27. p->funct(); //The above statement decides which class's function is to be invoked.
  28. *p=&d2; // Again vlaue of the pointer changes.
  29. p->funct(); //The above statement decides which class's function is to be invoked.
  30. return 0;
  31. }
Here the value of the pointer changes as the program is in execution and the value of the pointer decides which class’s function will be invoked. So here, the information is provided at run time, it takes the time to bind the data which slow downs the execution.

Key Differences Between Static and Dynamic Binding.

  1. Events that occur at compile time like, a function code is associated with a function call or assignment of value to a variable, are called static/early binding, and when these tasks are accomplished during runtime they are called dynamic/late binding.
  2. ‘Efficiency’ increases in static binding, as all the data is gathered before the execution. But in dynamic binding, the data is acquired at runtime so we can decide what value to assign a variable and which function to invoke at runtime this make execution ‘flexible’.
  3. ‘Static binding’ make execution of a program ‘faster’ as all the data needed to execute a program is known before execution. In ‘dynamic binding’ data needed to execute a program is known to the compiler at the time of execution which takes the time to bind values to identifiers hence, it makes program execution slower.
  4. Static binding is also called early binding because the function code is associated with function call during compile time, which is earlier than dynamic binding in which function code is associated with function call during runtime hence it is also called late binding.

Conclusion:

However, we conclude that when we have the prior knowledge of the values of variable and function calling, we apply static binding whereas, in dynamic binding, we provide all the information at the time of execution

No comments:

Post a Comment

commnet here