See “Defining a Class” (page 31) for more information on referring to instance variables.
Polymorphism
As the earlier examples illustrate, messages in Objective-C appear in the same syntactic positions as function
calls in standard C. But, because methods “belong to” an object, messages don’t behave in the same way
that function calls do.
In particular, an object can be operated on by only those methods that were defined for it. It can’t confuse
them with methods defined for other kinds of object, even if another object has a method with the same
name. Therefore, two objects can respond differently to the same message. For example, each kind of object
that receives a display message could display itself in a unique way. A Circle and a Rectangle would
respond differently to identical instructions to track the cursor.
This feature, referred to as polymorphism, plays a significant role in the design of object-oriented programs.
Together with dynamic binding, it permits you to write code that might apply to any number of different
kinds of objects, without you having to choose at the time you write the code what kinds of objects they
might be. They might even be objects that will be developed later, by other programmers working on other
projects. If you write code that sends a display message to an id variable, any object that has a display
method is a potential receiver.
Dynamic Binding
A crucial difference between function calls and messages is that a function and its parameters 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 invoked to respond to a message can be
determined only at runtime, not when the code is compiled.
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 “Messaging” in Objective-C
Runtime Programming Guide.)
This dynamic binding of methods to messages works hand in hand with polymorphism to give object-oriented
programming much of its flexibility and power. Because each object can have its own version of a method,
an Objective-C statement can achieve a variety of results, not by varying the message but by varying the
object that receives the message. Receivers can be decided as the program runs; the choice of receiver can
be made dependent on factors such as user actions.
When executing code based upon the Application Kit (AppKit), for example, users determine which objects
receive messages from menu commands such as 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
to a copy message than a Rectangle would. Because messages do not select methods until runtime (from
another perspective, because binding of methods to messages does not occur until runtime), these differences
in behavior are isolated to the methods themselves. The code that sends the message doesn’t have to be
concerned with them; it doesn’t even have to enumerate the possibilities. An application’s objects can each
respond in its own way to copy messages.
18
Object Messaging
2011-10-12 | © 2011 Apple Inc. All Rights Reserved.
CHAPTER 1
Objects, Classes, and Messaging