Dynamic Binding - Objective C
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. The exact code executed is determined by both the method name (the selector) and the receiving object.
Dynamic binding enables polymorphism. For example, consider a collection of objects including Rectangle and Square. Each object has its own implementation of printArea mehtod.
In the following code fragment , the actual code that should be executed by the expression [anObject printArea] is deternmined at runtime. The runtume system uses the selector for the method run to identify the appropriate method in whatever class of anObject turns out to be.
Let us look at a simple code that would explain dynamic binding.
As you can see in the above example , printArea method is dynamically selected in runtime. It is an example for dynamic binding and is quite useful in many situations when dealing with similar kind of objects.
In Objective C all methods are resolved dynamically at runtime. The exact code executed is determined by both the method name (the selector) and the receiving object.
Dynamic binding enables polymorphism. For example, consider a collection of objects including Rectangle and Square. Each object has its own implementation of printArea mehtod.
In the following code fragment , the actual code that should be executed by the expression [anObject printArea] is deternmined at runtime. The runtume system uses the selector for the method run to identify the appropriate method in whatever class of anObject turns out to be.
Let us look at a simple code that would explain dynamic binding.
As you can see in the above example , printArea method is dynamically selected in runtime. It is an example for dynamic binding and is quite useful in many situations when dealing with similar kind of objects.
No comments: