Chapter 6. Messages

Objects communicate via messages. Messages can ask to carry out an activity. They may also carry information from one object to another object.



a message is the vehicle by which a sender obejct obj1 conveys to a target object obj2 a demand for object obj2 to apply one of its methods

obj1 and obj2 may be the same objects, so objects may send messages to themselves.

We will see:

  • the anotomy of a message

  • the characteristics of message arguments

  • the role of an object sending the message

  • the role of an object receiving the message

  • the 3 types of messages

Message structure

In order for obj1 to send a message to obj2, obj1 must know 3 things:

  1. The handle of obj2. (You need to know who to talk to). obj1 will usually store obj2's handle in one of its variables (remember the section called “Associations”)

  2. The name of the operation of obj2 that obj1 wishes to execute

  3. Any additional information (arguments) that obj2 requires to execute the operation



the object sending the message (obj1 in this example) is called the sender.


the object receiving the message (obj2 in this example) is called the target

Example from the Hominoid:

hom1.turnRight;      // Books notation
hom1->turnRight();   // C++ notation

hom1 points to (contains the handle of) the target object of the message. turnRight is the name of the operation. In this case, turnRight has no arguments.

The sender in this case the the object from which this line is called.

Compare to procedural programming:

call turnRight(hom1);   // Books notation
turnRight(hom1);        // C++ notation

Traditional: the operation is important, the data is just an argument. What do I want to do? Oh, and by the way, this is what you need to do it with.

OO: the target is important, the message is second. Hey, you object! Do this!

More like real life: If you shout out "someone should turn on the lights", nothing will happen. They you call on a certain person, and then they may wake up and do it... However, if you point at someone and say "you! please turn on the light" it magically happens.

Reasoning: Different objects may provide the same operation.

Example operation: sitOnIt() may be provided by the classes Table, Chair, and Floor. Both are different objects, and the actual method is very likely differnt.

Example: operation: takeMeToSchool() may be provided by the classes Car, Truck, SchoolBus, Bike, Feet, Helicopter.