Hello, everyone. This second sequence on inheritance
will teach you more about behavior inheritance
and what happens when you send a message to an object.
The goal of this sequence
is to understand the message-sending algorithm
that makes it possible to convert a message
sent by a receiver to a method.
That is, what method will be run, in relation to the message sent,
and 'self" semantics, or what self really means.
As we saw in sequence 1,
inheritance is static, for state.
That is, when the subclass is defined,
its state is known.
But inheritance of behavior is dynamic.
When the program is running,
when you send a message to an object
you look for methods for class and superclass.
Message-sending is a two-step process.
First, you look up the method that matches the message.
Step two, you execute the method on the receiver.
So here, we have the Rectangle class,
the ColoredRectangle class,
and an instance of ColoredRectangle called aColoredRectangle,
We send the message "area" to this object.
Our lookup algorithm looks for a method matching the "area" message.
It finds the method
and carries it out
in relation to the object aColoredRectangle.
The lookup algorithm
identifies the method to be executed
when a message is sent to an object.
There are clearly two steps: message sending, and method execution.
They are fundamentally different, in object programming.
Even if you use another language,
it is important to understand the difference between the two.
If you send the "area" message to the aColoredRectangle object,
the first thing the lookup algorithm will do
is search for the class of the receiver.
The receiver of the message "area"
is aColoredRectangle.
The lookup algorithm starts searching for a method
in the object's class: ColoredRectangle.
It looks for an "area" method in that class.
The lookup algorithm
will not find the method "area" in that subclass.
So it will look elsewhere.
It looks in the superclass Rectangle.
It finds the method "area" there,
and returns it.
The lookup algorithm is done when it finds a method.
Let's study two cases, and review the algorithm I just listed on them.
We send the message "color"
to aColoredRectangle.
The algorithm searches the receiver class
for a method called color.
It finds it, and returns it.
If we now send the message "area" to aColoredRectangle,
the algorithm searches the receiver's class
for the method "area."
Not finding it, it searches the superclass.
It finds it, and returns the method.
"Self" always represents the receiver.
The following examples will make it clear to you
that the lookup algorithm always applies to the receiver self.
Here we have an instance of class A.
We send the message "foo."
The lookup algorithm searches for foo,
finds it, and executes it.
For B new, we start with aB,
the same thing as B new.
We search for a method, "foo."
The lookup algorithm finds it,
executes it, and returns 50.
So, here we have 10,
and 50.
It's important for you to take the time to think about what "self" means.
It is important to describe two points.
What does "self" represent?
How does message sending behave
when a message is sent to "self"?
"self" and "this" - "this" being the term in Java -
are the same thing.
They always represent the receiver of the message.
So, we saw this example
with A new foo and B new foo.
Now we'll send the message "bar."
Bar is sent to an instance.
Bar is found here, and executed.
and we send the message foo to object self.
"Self" is this object,
A new.
So we search this object for a method "foo."
We search the class for the method.
We find it, execute it, and return 10.
The other case is exactly identical.
But pay attention, because many beginning object programmers
get it wrong.
But the algorithm is identical.
We send "bar" to this object.
bar is not found here.
It is found here. So this is the bar that is executed.
At that point, the message "foo" is sent to self, which is...
aB, thus B new.
We send the message foo,
so the algorithm looks for the method in the object class.
It finds it.
50 is returned.
This is how the algorithm unfolds.
Look at each step in the process. It is what I just told you.
This example is also identical
to the preceding one.
Class B, here, is now empty.
A new class, C, is defined with the method foo return of 50.
You go through the algorithm process I just presented.
Here, you will get 10.
This is the foo that is executed, in the end.
Here, you get 50.
To conclude:
"Self," like "this" in Java, always represents the receiver.
Message sending always involves two steps.
First, the lookup algorithm looks for a match between the method
and the message sent.
Second step: the method found is executed on the receiver.
The lookup algorithm travels up from the receiver's class.
The goal of the algorithm lookup
is to find a method that matches the message sent to a receiver.