Hello. Welcome to the fourth sequence on inheritance and lookup.
We will now learn what happens when the algorithm does not find a method.
As you will see,
the class codes can add processing in this case,
to handle the fact that a lookup message
was not understood.
I'll review message-sending briefly:
The "area" message is sent to ColoredRectangle.
First, the algorithm looks up the method matching the message.
It starts by searching for the method in the receiver's class:
If it finds "area" in ColoredRectangle, it executes it.
But "area" is not there, so the algorithm searches the superclass
In "Rectangle," it finds "area."
The method is executed on the receiver.
What happens in the total absence of an "area" method?
What if the lookup fails?
Here is an example: a "coucou" message is sent to Object Node 1.
"Coucou" cannot be found in the hierarchy.
The "coucou" message is sent to Node 1.
The algorithm fails to find "coucou" in "Node."
Next, the algorithm searches "Object." No "coucou"
At that point,
the Pharo virtual machine sends a "does not understand" message
to the receiver - Node 1.
DNU = Does Not Understand.
It transfers the initial message as a parameter.
This is the parameter: "coucou stef."
If I go through it again step by step,
"Coucou" is sent to Node 1. A match is sought in the class,
then in the superclass.
"Coucou" is not there, so the message "does not understand" is resent
to Object Node 1.
The algorithm looks up the method "does not understand."
in Node 1 class.
Not finding a match there, the algorithm searches the superclass.
It is located there.
A "does not understand" method is found in Object and executed.
That means
in all the subclasses of Object,
"does not understand" can be implemented
to trigger a specific behavior
when an object, instance, or class does not understand a message.
This slide shows the details,
step by step.
"Does not understand" is a message like any other.
Therefore, you can implement a "DNU" method to be executed
whenever an instance does not understand a message.
This mechanism is used
in certain somewhat complicated cases
like proxies and automatic delegation:
when you want a certain object to transmit all the messages it gets
to another object.
The "does not understand" mechanism can be used for that.
Default implementation of "does not understand" is found
in the Object class.
It raises one exception.
The exception
is called "Message Not Understood."
So if the "DNU" method of the Object class
is executed, an exception is raised.
That means the code you are writing can catch the exception,
the way it would catch any exception.
The exception opens a debugger for messages that are not understood.
In this example,
we return to the class "Node."
We have a "say hello" method in Node, which sends a "coucou" message
to the receiver or self.
There's also a "welcome" method
that sends a "say hello" message to self.
It provides special handling in the context
of a misunderstood message.
If a message is not understood, it will be handled.
Here, "say hello" sends a "coucou" message to self.
"Coucou" is still unimplemented.
Ultimately, the "does not understand" method
will be executed.
The "message not understood" exception will be raised.
The code here, following on/do,
is used to catch exceptions, like "try/catch" in Java.
A later sequence of the course will go into these exceptions.
The point here is to notice that this code
is the same as Java's "catch," and it will be executed for every exception.
Say we send the message "welcome"
to Node 1.
Knowing that "coucou" is not implemented,
the console will display "something went wrong with the message."
What you should know:
When the lookup algorithm does not find a method to match a message,
a "does not understand" message is sent to the initial receiver,
with the first message as a parameter.
Each class can implement this method.
The default implementation in the Object class will be executed.
This default implementation raises an exception,
called "message not understood."
The code you are writing can catch the exception
and subject it to debugging.