"Learning Object-Oriented Programming and Design with TDD"
"First look at Objects, Classes, Methods and messages"
Stéphane Ducasse, research director, Inria.
-In this lecture, I will do a survey of all the concepts
that we will see in future lectures.
So, we will look at what objects, classes, methods and messages are.
So, what is an object?
In fact, an object is a small living entity
in the object-oriented world,
and this entity is characterized by three properties,
a state, a behavior and an identity.
So, let us look at an example.
If we take a Logo turtle...
A Logo turtle is a small entity, an object.
It has a state. For example, the pen, if its pen is up or down,
the size of the pen, the color and the direction.
And the turtle also has a behavior.
This means that it knows how to draw lines on the floor,
jump or hide itself.
But also, these turtles have an identity,
and the idea is that, when you look at the picture,
you have two turtles, one spider and one turtle,
and they are different entities, the spider is not the turtle.
Now, in fact, in the object-oriented programming paradigm,
everything is an object,
so what you will see is that even a file that you have on your disk is an object.
It is represented by a path and a content.
That is the state of the object.
The behavior is...
For example, you can copy your file, or you can rename the file.
And the identity is clearly that a file is different from another one.
Else, you would have only one on your hard disk.
Now, I would like to say something
about the difference between identity and equality.
This can be confusing, but here is something really simple.
Imagine that you enter a pizzeria and you want to order a pizza.
There is a guy eating a pizza, and you want the same.
Identity would mean that you go and eat his own pizza,
whereas equality means that you want to eat the same pizza.
So, you see that identity is the pointer to the object,
whereas equality is something that is similar to the object.
Now, let us go back to the object.
An object reacts to messages, and it will perform an action.
So, what I show on the slide is that you have a turtle,
and then, you send a message to the turtle
to go to a certain location.
Then, you send the message "go to: 100",
and then, the turtle will go and leave a trace on the floor.
Now, what we should see
is that different objects can understand the same message.
Each object can perform a different reaction,
but this is still the same object.
When we have "aWindow open", we will open a window.
When we have "aFile open", we will get the content of the file.
And if you have "aZip open", you will get the content of the Zip.
Now, in the vocabulary,
it is really important to understand what the message receiver is.
The message receiver
identifies the object that actually receives the message.
For example, when I say "aTurtle jumpTo: aLocation",
I send the message "jumpTo" to a turtle, and this is the receiver.
And similarly, if I say "aWindow open", the receiver is a window,
and it is the same for "aFile open".
Now, what is a method? It is in fact a sequence of instructions,
and we give it a name.
So, let us take the example of crepes.
To make crepes,
we know that we need eggs, milk, flour and some sugar.
This is a recipe, and a method is a recipe.
It is a named sequence of instructions
that the object will execute in response to a message.
So, what is the difference between a message and a method?
A message is what to do. It is a kind of order.
"stef makeCrepes", "robert makeCrepes"...
Those are orders, and a method is how to do it.
It is a list of steps.
So, the message is what to do, whereas the method is how to do it.
It is important to understand that we can have one message
and multiple methods with the same name.
When I have "Window >> open",
I will have a specific sequence to open a window.
When I have "File >> open",
I will have a specific sequence to open a file.
And still, they have the same message, they understand the same message.
Now, let us look at classes, because objects come from somewhere.
Objects come from classes, and a class is like a factory of objects.
A class acts as a mold for objects.
If you check the picture on the slide,
this is a bathtub-making system.
You put some steel and you get a bathtub.
What is important is that the class will create many instances,
but each instance is still a separate entity.
This means that you can destroy one bathtub,
but you will not destroy the other ones.
Each entity has its own identity.
Still, they all are instances of a class.
Now, if you look at it from another perspective,
a class will define all the variables of its instances,
and it will define the behavior that the instances should have.
So, how do we create an object?
In fact, we send the message "new" to a class.
For example, here, I have a class turtle and I ask it:
"I want to have a new instance, can you create one for me?"
I will send the message "new", and I will get a new turtle.
And that is the same for every object in the system.
For a file, if I send the message "new", I will get a file.
What is important to understand is that methods are dynamically looked up
in the class of the receiver.
So, here, I show three different kinds of classes and objects.
I have a dog, instance of the class "dog",
a cat, instance of the class "cat"
and a fish, instance of the class "fish", and they can be afraid.
But the dog will react differently from the cat.
This means that when I send the message "angry" to a dog,
I will look in the class "dog"
to find the correct methods that will be executed.
And this is the same.
I have one message that is "angry", and I will send it to multiple instances,
and they will look respectively in their class
to find the correct behavior, the correct methods to be executed.
So, with that, we have basically covered
all the most important concepts in object-oriented programming.
An object is a unique entity and an instance of a class.
A class specifies the shape and the behavior
of all its instances.
An object receives messages
and executes corresponding methods.
A message is an order
and a method is a sequence of instructions.
And different objects can understand the same message
but react differently.
This is what we will see in future lectures.