Quick Index

Overview


Objects send messages to one another.

That's how they move and shake and make oop systems work.

Before starting, we want to understand the big three.

We have an object.

However, it's just sitting there. The data is private, so we can not access it.

For example if the object is a calculator we can't even ask it to calculate!
How do we kick things into action?

Let us say the object has some "methods" where it can "receive messages" (identified as "M" in the figure).

This allows us to send messages to the object.

Now activity can happen.
Object "A" sends message "start" to object "B".
Object "B" receives the message and handles in its method "start".

Later, object "A" sends message "stop" to object "B".
Object "B" receives the message and handles in its method "stop".
An oop system is a "universe" consisting of objects sending and receiving messages.

Message Code Syntax


Left is a message sender object.

Right is a message target (or receiver) object.

The message goes from the sender to the target.
This is what the code would look like the example message send.

Note that we are coding inside the 'drawing' (the sender class).


circle.show();
We now have a general pattern we can always use to send a message:

target-dot-msg

Syntax may vary slightly depending on the algorithm/programming code type. Message params/args are not shown here for conciseness. We'll learn about these soon.


<target><dot><msg>()

target = the target (receiver) object of the message
dot = .
msg = the message name
This is the target-dot-msg pattern for the example.
<target><dot><msg>()

circle.show();

target = "circle"
dot = .
msg = "show"


Example Message Sending


A few objects hanging out.

All is quiet.
Our program runs and the message sending begins.

Objects are sending messages to one another.

The drawing has three shapes it is sending messages to.

Then the sub-objects may also send more messages -- one example is shown where a Circle sends the message "move" to it's "center" (which is a "Point" object).
Using "target-dot-msg" we write code to send messages:

rec.show();
elli.hide();
circ.move();


Where:

target = "rec"
dot = .
msg = "show"
(the other two message sends are similar)


Special Case: Messages to Self (this)


This is a special case of our basic "target-dot-msg" where target is 'this' (meaning the object you are coding in).

When we send a message to self we call the target 'this'.

Some languages (e.g. Python, Smalltalk) call the target 'self'.
Again using "target-dot-msg" we write code to send messages:

this.moveAll();
this.showAll();


Where (we show "moveAll" here, and "showAll" would be similar):

target = "this"
dot = .
msg = "moveAll"