Dynamic Binding
A crucial difference between function calls and messages is that a function and its arguments are
joined together in the compiled code, but a message and a receiving object aren’t united until the
program is running and the message is sent. Therefore, the exact method that’s invoked to respond
to a message can only be determined at runtime, not when the code is compiled.
The precise method that a message invokes depends on the receiver. Different receivers may have
different method implementations for the same method name (polymorphism). For the compiler to
find the right method implementation for a message, it would have to know what kind of object the
receiver is—what class it belongs to. This is information the receiver is able to reveal at runtime when
it receives a message (dynamic typing), but it’s not available from the type declarations found in
source code.
The selection of a method implementation happens at runtime. When a message is sent, a runtime
messaging routine looks at the receiver and at the method named in the message. It locates the
receiver’s implementation of a method matching the name, “calls” the method, and passes it a pointer
to the receiver’s instance variables. (For more on this routine, see “How Messaging Works” (page
75).)
The method name in a message thus serves to “select” a method implementation. For this reason,
method names in messages are often referred to as selectors.
This dynamic binding of methods to messages works hand-in-hand with polymorphism to give
object-oriented programming much of its flexibility and power. Since each object can have its own
version of a method, a program can achieve a variety of results, not by varying the message itself,
but by varying just the object that receives the message. This can be done as the program runs; receivers
can be decided “on the fly” and can be made dependent on external factors such as user actions.
When executing code based upon the Application Kit, for example, users determine which objects
receive messages from menu commands like Cut, Copy, and Paste. The message goes to whatever
object controls the current selection. An object that displays text would react to a copy message
differently from an object that displays scanned images. An object that represents a set of shapes
would respond differently from a Rectangle. Since messages don’t select methods (methods aren’t
bound to messages) until runtime, these differences are isolated in the methods that respond to the
message. The code that sends the message doesn’t have to be concerned with them; it doesn’t even
have to enumerate the possibilities. Each application can invent its own objects that respond in their
own way to copy messages.
Objective-C takes dynamic binding one step further and allows even the message that’s sent (the
method selector) to be a variable that’s determined at runtime. This is discussed in the section “How
Messaging Works” (page 75).
Dot Syntax
Objective-C provides a dot (.) operator that offers a compact and convenient syntax you can use as
an alternative to square bracket notation ([]s) to invoke accessor methods. It is particularly useful
when you want to access or modify a property that is a property of another object (that is a property
of another object, and so on).
18
Object Messaging
2008-07-08 | © 2008 Apple Inc. All Rights Reserved.
CHAPTER 1
Objects and Classes