We are independent & ad-supported. We may earn a commission for purchases made through our links.
Advertiser Disclosure
Our website is an independent, advertising-supported platform. We provide our content free of charge to our readers, and to keep it that way, we rely on revenue generated through advertisements and affiliate partnerships. This means that when you click on certain links on our site and make a purchase, we may earn a commission. Learn more.
How We Make Money
We sustain our operations through affiliate commissions and advertising. If you click on an affiliate link and make a purchase, we may receive a commission from the merchant at no additional cost to you. We also display advertisements on our website, which help generate revenue to support our work and keep our content free for readers. Our editorial team operates independently of our advertising and affiliate partnerships to ensure that our content remains unbiased and focused on providing you with the best information and recommendations based on thorough research and honest evaluations. To remain transparent, we’ve provided a list of our current affiliate partners here.
Software

Our Promise to you

Founded in 2002, our company has been a trusted resource for readers seeking informative and engaging content. Our dedication to quality remains unwavering—and will never change. We follow a strict editorial policy, ensuring that our content is authored by highly qualified professionals and edited by subject matter experts. This guarantees that everything we publish is objective, accurate, and trustworthy.

Over the years, we've refined our approach to cover a wide range of topics, providing readers with reliable and practical advice to enhance their knowledge and skills. That's why millions of readers turn to us each year. Join us in celebrating the joy of learning, guided by standards you can trust.

What is Dynamic Binding?

By Samer Adra
Updated: May 16, 2024
Views: 11,554
Share

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.

Share
EasyTechJunkie is dedicated to providing accurate and trustworthy information. We carefully select reputable sources and employ a rigorous fact-checking process to maintain the highest standards. To learn more about our commitment to accuracy, read our editorial process.
Discussion Comments
Share
https://www.easytechjunkie.com/what-is-dynamic-binding.htm
Copy this link
EasyTechJunkie, in your inbox

Our latest articles, guides, and more, delivered daily.

EasyTechJunkie, in your inbox

Our latest articles, guides, and more, delivered daily.