-Hello. In this lecture, we will look at what classes are.
Basically, a class is a factory of objects.
But more precisely, a class is a mold of objects.
What you see on the slide is that we have this big machine
that creates bathtubs.
And what you see is that this machine will create many bathtubs.
Probably thousands or hundreds of thousands of them.
A class will also create hundreds of thousands of objects,
and all these objects are still autonomous.
This means that you can take an object,
and this object can have specific properties,
a state that is not shared by the other objects.
But since they are all created by the same class,
all these objects will share the same behavior.
So, what we see is that in fact, a class defines all the variables
and the characteristics that an object will have.
And all its instances will have the same characteristics.
And a class defines the behavior,
which will be shared by all these objects.
The idea is that the object should not define all the same methods.
These methods are shared by all the instances.
This way, we optimize space
and we do not have to have thousands of similar methods in the system.
A class factorizes object properties.
For example, all rectangles will have a height and a width,
but a given rectangle will have a value for the height and the width
different from another rectangle.
We will define the behavior once,
and all the objects will be able to reuse this behavior.
In fact, all the instances of the class will reuse this behavior.
This is what I show with the definition of the method "top".
All the rectangles will be able to answer
and execute this method in reaction to a message.
What I show in this UML diagram is that, for example,
rectangles, here, are represented by two points,
the origin and the corner.
And they offer multiple methods to the system,
like "area", "topCorner", etc.
Now, what is really important
is that each of the rectangles
that are instances of the class "rectangle"
has different properties.
You can have a rectangle that is flat, one that looks like a square,
or one that is really tall.
They all have a width and a height, and they will share the same behavior,
but they have different values,
so each instance will have a specific value.
This is what I said in this slide.
The class defines the properties
that all of its instances should hold,
but not the actual value.
Each instance will have its own specific value.
And then, the class defines the behavior
that is shared among all the instances of the class.
If you look at the differences between classes and instances...
An object is an instance of a class.
It has been created by a class.
A class will specify the behavior
that all its instances will comply to or execute,
and this behavior is shared among all the instances of a class.
And a class will specify the structure of the instance,
but not the value that the instance should hold.
So, what we should see is that the instances of a class
all share the same behavior. How does it work?
In fact, when I send a message to one rectangle,
I will look for the methods in the class of the rectangle,
and I will find a method which will be applied on this object.
That is why, if I send a message to another rectangle,
I will find the same method and apply it to this other rectangle.
This way, I share the behavior.
So, all the instances of a class will share the same behavior.
What we should see is that methods are looked up dynamically.
This means that I will look for a method in the class of the object.
Here is an example with dogs, cats and fish.
When I send the message "angry" to a dog,
I will look in the class "dog",
find the definition of the method and execute it.
And this behavior is different from the one in the class "cat".
But all the dogs will execute the same method.
And here again, this is the same message
that I will send to different instances of different classes
and that will execute different methods.
So, when I send the message "angry" to an instance of the class "cat",
I will not execute the same method
as when I send the message "angry" to an instance of the class "dog".
So, what you should know is that any object is an instance of a class.
A class is a factory of objects.
It describes the state of an object, of every of its instances.
And it defines the behavior that all its instances will share.