ref -http://www.tutorialspoint.com/objective_c/objective_c_dynamic_binding.htm
Dynamic binding is determining the method to invoke at runtime instead of at compile time. Dynamic binding is also referred to as late binding.
In Objective-C, all methods are resolved dynamically at runtime, due to having every class must derive from NSObject. The exact code executed is determined by both the method name (the selector) and the receiving object. On the other hands, functions such as NSLog or abs, fabs, etc are statically bound. They are determined during compile time.
Dynamic binding enables polymorphism. For example, consider a collection of objects including Rectangle and Square. Each object has its own implementation of a printArea method.
In the following code fragment, the actual code that should be executed by the expression [anObject printArea] is determined at runtime. The runtime system uses the selector for the method run to identify the appropriate method in whatever class of anObject turns out to be.
Parent class – Shape
1 2 3 4 5 6 7 |
@interface Shape : NSObject { } @end @implementation Shape @end |
Child class Square
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
@interface Square: Shape { float area; } - (void)calculateAreaOfSide:(CGFloat)side; - (void)printArea; @end @implementation Square - (void)calculateAreaOfSide:(CGFloat)side { area = side * side; } - (void)printArea { NSLog(@"The area of square is %f",area); } @end |
Child class – Rectangle
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
@interface Rectangle: Shape { float area; } - (void)calculateAreaOfLength:(CGFloat)length andBreadth:(CGFloat)breadth; - (void)printArea; @end @implementation Rectangle - (void)calculateAreaOfLength:(CGFloat)length andBreadth:(CGFloat)breadth { area = length * breadth; } - (void)printArea { NSLog(@"The area of Rectangle is %f",area); } @end |
Main
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
int main() { Square *square = [[Square alloc]init]; [square calculateAreaOfSide:10.0]; Rectangle *rectangle = [[Rectangle alloc]init]; [rectangle calculateAreaOfLength:10.0 andBreadth:5.0]; //we put the child classes in an array NSArray *shapes = [[NSArray alloc]initWithObjects: square, rectangle,nil]; //if then use generic class pointer for them and access printArea method, it will dynamically //bind to the correct printArea Shape * object1 = [shapes objectAtIndex:0]; [object1 printArea]; Shape * object2 = [shapes objectAtIndex:1]; [object2 printArea]; return 0; } |
Now when we compile and run the program, we will get the following result.
2013-09-28 07:42:29.821 demo[4916] The area of square is 100.000000
2013-09-28 07:42:29.821 demo[4916] The area of Rectangle is 50.000000
The parent class pointer will know which method to execute because it will dynamically bind the correct method to the object at hand. Note that we can also do it with
1 2 |
id object1 = [shapes objectAtIndex:0]; [object1 printArea]; |
because id is a generic pointer.
Namely, ff you have a reference that’s of parent type at compile type, you can assign a child type to it at runtime. The behavior of the reference will change to the appropriate type at runtime. A virtual table lookup will be done to let the runtime figure out what the dynamic type is.
Another exaplantion:
Dynamic binding – This is a step beyond late binding where it is left to runtime to determine if the referenced item exists. A simple example in Objective-C:
1 2 3 4 5 6 7 8 9 |
id anyObject; // this can hold a reference to any Objective-C object NSUInteger len = [anyObject length]; // TRY to call a method length on the object // referenced by anyObject. If at runtime this is, // say, an NSString or NSMutableString value it will // succeed. However if it is, say, an NSNumber value // it will FAIL Like late binding, the actual method that gets called is not determined until runtime; however, unlike late binding, whether such a method exists is also left to runtime. So, unlike both static and late binding, dynamic binding can fail at runtime. |
Static Binding
Static, compile time binding is easy. There’s no polymorphism involved. You know the type of the object when you write and compile and run the code. Sometimes a dog is just a dog.
Static binding: is where the item being referred to is determined at compile time. In (Objective-)C(++) a call to a function is statically bound; for example the library functions fabs and NSLog.
Also references to variables are static in these languages; which variable is being referenced is determined completely at compile time. Static binding cannot fail at runtime, it is a compile time error to fail to determine what a statically bound reference refers to.