Learn something new every day More Info... by email
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.
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.
One of our editors will review your suggestion and make changes if warranted. Note that depending on the number of suggestions we receive, this can take anywhere from a few hours to a few days. Thank you for helping to improve wiseGEEK!