Category: 

What Is Dynamic Binding?

In dynamic binding, the object mapped by a function is not known at compile time and can be determined only during program run time.
Article Details
  • Written By: Samer Adra
  • Edited By: A. Joseph
  • Last Modified Date: 14 October 2014
  • Copyright Protected:
    2003-2014
    Conjecture Corporation
  • Print this Article
Free Widgets for your Site/Blog
In 18th-century England, murderers could ask for a lesser sentence by claiming a full moon made them act erratic.  more...

November 20 ,  1945 :  The Nuremberg Trials began.  more...

In computer science, name binding is the association of an identifier, such as a function or variable name, to a section of code or data. In the most common scenario, static binding, this mapping is known at compile time. In dynamic binding, the object mapped by a function is not known at compile time and can be determined only during program run time. For this reason, it is also called late binding. Although it offers flexibility not available with static binding, it also entails more performance costs than static binding.

Dynamic binding is closely related to polymorphism, which is part of object-oriented programming. Polymorphism allows the same method name to be implemented in different ways. If the code is not written in such a way that the precise method cannot be determined at compile time, then dynamic binding must be used.

Ad

For example, a "Shape" class might have a method called "GetArea," because every shape has an area. A "Circle" subclass of "Shape," however, would implement "GetArea" differently from the way a "Square" subclass would. Therefore, if a new object is created of type "Shape," and if code calls the method "GetArea" on that shape, the compiler will have no way of knowing whether the shape will end up being a circle or a square, and therefore it will not know which GetArea method to call. This is an example of dynamic binding, because the correct GetArea method will be mapped only at run time, after the program knows what kind of shape the object is.

Dynamic binding allows the flexibility of using abstract methods without knowing which specific implementation will be used. In the "Shape" example, the code could be written to avoid dynamic binding by explicitly using this logic: If the shape is a circle, then call the circle's GetArea method; else, if the shape is a square, call the specific GetArea method for squares. The benefit of dynamic binding is that the code is cleaner and more maintainable than the alternative. In the static binding example, there is code duplication, and the code must be updated anytime a new type of shape is added.

The downsides are performance and safety. In static binding, the compiler knows exactly what code to call and can optimize the code to run more efficiently. Type safety can be an issue because, in some implementations of dynamic binding, a method can be called on an object that does not support the method. For example, the "GetArea" method might be called on an object that is not a shape and therefore has no "GetArea" method, which could result in a run-time error. Static binding would prevent this scenario by raising a compile error.

Ad

More from Wisegeek

You might also Like

Discuss this Article

Post your comments

Post Anonymously

Login

username
password
forgot password?

Register

username
password
confirm
email