Open In App

Dynamic Binding in Objective-C

Last Updated : 17 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 Binding

We 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




// Objective-C program for dynamic binding
#import <Foundation/Foundation.h>
  
@interface Circle:NSObject {
   float area;
  // Creating a object by NS 
}
  
- (void)calculateArea:(CGFloat)R;
- (void)displayArea;
@end
  
// Finding area of circle
@implementation Circle
- (void)calculateArea:(CGFloat)R {
   area = 3.14 * R * R;
}
  
- (void)displayArea {
   NSLog(@"Area of the Circle is %f",area);
}
  
@end
  
@interface Rectangle:NSObject {
   float area;
}
  
- (void)calculateAreaOfRect:(CGFloat)length andBreadth:(CGFloat)Breadth;
- (void)displayArea;
@end
  
@implementation Rectangle
  
// Finding area of rectangle
- (void)calculateAreaOfRect:(CGFloat)length andBreadth:(CGFloat)Breadth {
   area = length * Breadth;
}
  
- (void)displayArea {
   NSLog(@"Area of Rectangle is %f",area);
}
  
@end
  
int main() {
  
   Circle *circle = [[Circle alloc]init];
   [circle calculateArea:9.0];
     
   Rectangle *rectangle = [[Rectangle alloc]init];
   [rectangle calculateAreaOfRect:14.0 andBreadth:5.0];
     
   NSArray *shapes = [[NSArray alloc]initWithObjects: circle, rectangle, nil];
   id object1 = [shapes objectAtIndex:0];
   [object1 displayArea];
     
   id object2 = [shapes objectAtIndex:1];
   [object2 displayArea];
     
   return 0;
}


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




// Objective-C program for dynamic binding
#import <Foundation/Foundation.h>
  
@interface Triangle:NSObject {
   float area;
}
  
- (void)calculateArea:(CGFloat)base andHeight:(CGFloat)height;
- (void)displayArea;
@end
  
@implementation  Triangle
  
// Finding area of triangle
- (void)calculateArea:(CGFloat)base andHeight:(CGFloat)height {
   area = (base * height)/2;
}
  
- (void)displayArea {
   NSLog(@"Area of Triangle is %f", area);
}
  
@end
  
@interface Square:NSObject {
   float area;
}
  
- (void)calculateAreaOfSqr:(CGFloat)side;
- (void)displayArea;
@end
  
@implementation Square
  
// Finding the area of square
- (void)calculateAreaOfSqr:(CGFloat)side {
   area = side * side;
}
  
- (void)displayArea {
   NSLog(@"Area of square is %f",area);
}
  
@end
  
int main() {
     
   Triangle *triangle = [[Triangle alloc]init];
   [triangle calculateArea:72.0 andHeight:5.0];
     
   Square *square = [[Square alloc]init];
   [square calculateAreaOfSqr:23.0];
     
   NSArray *shapes = [[NSArray alloc]initWithObjects: square, triangle, nil];
   id object1 = [shapes objectAtIndex:0];
   [object1 displayArea];
     
   id object2 = [shapes objectAtIndex:1];
   [object2 displayArea];
     
   return 0;
}


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

Dynamic Binding

Static Binding

It is basically defined as the method which is invoked at run time. It is basically defined as a method which hath is invoked at compile time of the program.
It is also called late binding. It is basically called early binding.
It will be applied to real objects basically real objects always use dynamic binding. It is not applied on real objects they will not use static binding.
It takes place using virtual functions It takes place using normal function calls, operator overloading, and function overloading.
It uses the concept of polymorphism. It does not use the concept of polymorphism.
Execution of dynamic binding is slower than static binding because the function call is not resolved until runtime. Execution of static binding is faster than dynamic binding because of all the information needed to call a function.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads