What’s dynamic binding?

Print anything with Printful



Name binding associates an identifier with code or data. Static binding is known at compile time, while dynamic binding is determined during program execution. Dynamic binding allows for polymorphism and abstract methods, but incurs higher performance costs and potential type safety issues.

In computing, name binding is the association of an identifier, such as a function or variable name, with 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 only be determined during program execution. For this reason it is also called late binding. While it offers flexibility not available with static binding, it also incurs higher 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 your code is not written in such a way that the precise method cannot be determined at compile time, dynamic binding should 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 than a “Square” subclass would. Thus, if a new object of type “Shape” is created and if the code calls the “GetArea” method 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 will not know which GetArea method to call. This is an example of dynamic binding, because the correct GetArea method will only be mapped at runtime, after the program knows the shape type of the object.

Dynamic binding allows for the flexibility to use abstract methods without knowing which specific implementation will be used. In the “Shape” example, code could be written to avoid dynamic binding by explicitly using this logic: if the shape is a circle, call the circle’s GetArea method; otherwise, if the shape is a square, call the square-specific GetArea method. 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 needs to be updated every time a new shape type is added.

The downsides are performance and security. In static binding, the compiler knows exactly which 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, it is possible to call a method on an object that does not support the method. For example, the “GetArea” method could be called on an object that is not a shape and therefore does not have a “GetArea” method, which could cause a run-time error. Static binding would prevent this scenario by throwing a compile error.




Protect your devices with Threat Protection by NordVPN


Skip to content