![]() |
As the name suggests Dynamic binding is the process of joining two different things or creating a relation between different things. Or we can say that dynamic binding is a process of determining a method to invoke at runtime, not at the compiler time. Objective – C is a programming language that supports all the concepts so dynamic binding. It is basically defined as the process of connecting the function call with the function definitions by neglecting the issue of static binding, which generally occurs at build time. Static binding has lots of limitations that it is only defined at the build time only that’s why we use dynamic binding because it is more flexible than static binding, it avoids all the drawbacks and limitations of static binding, which connected the function call and function definition at the built time but in dynamic binding, we can do it any time during the execution of the code. So, choosing a specific function to run up until runtime is known as Dynamic binding. A function will be invoked depending on the kind of item. Basically, it defines the method to invoke or call at the runtime of the program, instead of at the compile time. Usage of Dynamic BindingWe can use dynamic binding with a single function name to handle multiple objects. It made debugging the code and errors easier and reduced complexity. All the methods in Objective-C, are resolved dynamically at the program runtime. It is also referred to as late binding. The exact code executed is determined through the method name and the receiving object both. Let’s take some examples to understand the concept of Dynamic binding, It also enables Polymorphism in the programs. Example 1: Let’s take an example of a Circle and a square considering them as objects and we have to print the area in the output. Let’s see the code and its implementation. ObjectiveC
Output: Area of the Circle is 254.339996 Area of Rectangle is 70.000000 Example 2: Let’s take one more example to understand it better. So consider two objects one is a triangle and the other is a Square and in the output, we have to find out the area of both objects. Let’s see its code. ObjectiveC
Output: Area of square is 529.000000 Area of Triangle is 180.000000 As in above, both the programs have displayArea() method which is dynamically selected in runtime. So these were examples of dynamic binding and it is very useful in many situations over static binding when we deal with different types of objects in different situations. Dynamic Binding vs Static Binding
|
Reffered: https://www.geeksforgeeks.org
Objective C |
Related |
---|
|
![]() |
![]() |
![]() |
|
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 17 |