Spiegato, Home

Technology and Gadgets

What is Dynamic Binding?

  • Published by Spiegato
  • 2 min read

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.

Key terms in this article

Related articles

  • What is Late Binding?

    Late binding is a computer programing term that describes when a program accesses a portion of its code for the first time. Most programs are written as…

  • What is Data Binding?

    Data binding is a procedure that makes it possible for an end user to manipulate the information displayed on a web page, without the need to utilize any…

  • What Is Dynamic Imaging?

    The term “dynamic imaging” refers to a variety of imaging technologies, spanning personal and professional uses. The term usually relates to the…

  • What is Dynamic Programming?

    Dynamic programming, when referring to the field of computer science, describes a group of similar computer algorithms meant to solve complex problems by…

  • What Is Dynamic Memory Allocation?

    Dynamic memory allocation, also called heap-based memory allocation, is a type of memory management that is sharply contrasted by static memory…