Hello, everybody. In today's class, we are going to see
if you have understood our previous classes,
namely as regards super.
The idea is to make you think hard.
I have a test to see if you've understood.
What will you learn?
You'll think about what super is,
revisit message lookup,
and think about class methods.
It's a lot of material for one class, but it's fun.
Imagine that I defined a method in the dice class,
and redefined the "new" method as follows:
Here is "new" and here is the code.
inst := super new.
inst initialize. Return inst.
Now I execute the expression dice new.
Now my question for you is:
In this expression, what is inst?
What is super?
And what is super new?
I'll give you a few seconds to think it over.
I'll give you a few clues.
They're based on my teaching experience.
No, super is not the superclass.
Get that out of your heads.
No, inst is not an instance of the superclass.
If it was, we could never write instances for the classes below.
So what is it?
Let's take a look.
We said in the video on super
that super was the receiver of the message.
Like self, super points out the receiver of the message.
This is not specific to Pharo.
It's true in all object-oriented languages such as Java, C#,
and Smalltalk.
Here the message is dice new.
So what's the receiver? Syntactically speaking, it's dice.
The class is dice.
With these clues, you should understand better.
Let's see.
You remember how message lookup works?
I send a message to an instance,
for example, ColoredRectangle.
I look inside the ColoredRectangle class
to see if the method in question is defined. It's not.
"Area" is not defined. I find it up here.
I apply the definition I found.
On what? On the receiver.
Now you should understand what this method does.
I look up "new" in the dice class.
I look it up and apply it on dice.
Let's take a closer look.
As I explained previously,
in Pharo, only one message is sent with one method lookup.
So when I send a message to any object whatsoever,
I always look in the class. I follow this link.
Then this one here.
It's always these two steps.
What does this say about our system?
I told you, super is the receiver.
The receiver was dice.
So I look for new in the superclass of the dice class.
The key here is that it's dice class and not dice.
We'll see it graphically afterwards.
Once I've found the new "new,"
I apply it to the receiver, which is dice.
What does this do?
I apply new to dice,
which will create and initialize a new instance of dice.
Inst will be my new instance.
One dice.
And I will initialize and return it.
Let's take a look at a diagram.
When I send new to dice,
where do I look? Which class do I look in?
I look in dice class.
This is where I use my famous method.
What do I do now? What did we say about super?
Super is the dice class. It is the receiver.
I sent the message to this object.
Super tells me to look inside
the superclass of the class containing the expression.
So I look in the superclass of the dice class.
I look up here. And somewhere up here,
we'll see later on,
new is defined.
What about lookup?
We said that the method is defined here,
and we apply it on the receiver. I apply it on dice.
And this gives me a new instance. I'll draw it in.
It returns an instance: A dice.
And inst is pointed at this new instance.
So inst is a new dice.
I send the initialize message to inst, and then I return it.
You often struggle with this example because it's very rhetorical.
I do it on purpose to see if you've understood.
It mixes two things.
One, super.
I look in the superclass of the class that contains the expression super,
knowing that super is the receiver.
And two, new. It's a method that creates objects.
This is where the two meet,
and you have a hard time understanding this.
I suggest that you really review this lesson,
because it's very important.
I'll repeat it.
Super is the receiver of the message,
which in this case is dice.
I look in the superclass of the dice class for the new method
that I will apply to dice, the receiver, to find my terminal instance.
Okay?
In sum: Sending a message is looking up a method in the receiver class.
That's what we do each time.
That's all you can do in Pharo, so it's easy.
By now you know that super means looking in the superclass of the class
containing the expression super.
And that super is the receiver, but it can be a class too.
We're not done yet. I have another problem for you.
If you're studying Pharo, you must like challenges.
Here we go. Imagine that I have the following problem:
The method says: "Make super-space-class
"equal equal self class."
What does "equal equal" mean? It's the pointer's identity.
In fact, I'm asking: Is the object obtained
with super-space-class the same
as the object obtained with self-equal-class?
I defined this method, which will return true or false.
My question for you is:
What is the result of A new foo?
I create a new instance of A
and I send the message foo, which will edit this message.
What is the result and why?
You'll have to think because I won't give you this answer.