Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is difference between dynamic binding Vs dynamic typing in Objective C

Tags:

objective-c

I am having hard time to understand what is difference between dynamic binding Vs dynamic typing in Objective C. Can someone explain this ?

like image 447
AAV Avatar asked Feb 20 '12 16:02

AAV


People also ask

What is dynamic binding in 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.

What is dynamic binding difference between dynamic and static binding?

The static binding uses Type information for binding while Dynamic binding uses Objects to resolve to bind. Overloaded methods are resolved (deciding which method to be called when there are multiple methods with the same name) using static binding while overridden methods use dynamic binding, i.e, at run time.

What is dynamic type binding?

Dynamic binding or late binding is the mechanism a computer program waits until runtime to bind the name of a method called to an actual subroutine. It is an alternative to early binding or static binding where this process is performed at compile-time.

What is the difference between static and dynamic typing duck typing?

Structural type systems Structural typing is a static typing system that determines type compatibility and equivalence by a type's structure, whereas duck typing is dynamic and determines type compatibility by only that part of a type's structure that is accessed during run time.


1 Answers

Dynamic typing in Objective-C means that the class of an object of type id is unknown at compile time, and instead is discovered at runtime when a message is sent to the object. For example, in the following code, the class of foo isn't known until we attempt to send the message componentsSeparatedByString:.

id foo = @"One Two Three";
NSArray *a = [foo componentsSeparatedByString:@" "];

If instead of using the id data type we had done the following...

NSString *foo = @"One Two Three";

...then we'd be using static typing rather than dynamic typing.

Dynamic binding means that the compiler doesn't know which method implementation will be selected; instead the method implementation is looked up at runtime when the message is sent. It basically helps us with Polymorphism. So

[foo description]

results in invoking a different method implementation if, for example, foo is an instance of NSArray rather than an instance of NSString.

like image 109
jlehr Avatar answered Nov 16 '22 03:11

jlehr