Hello, everyone.
Welcome to Sequence 1 of Week 4.
This week, we'll be covering Pharo inheritance basics,
and look-up and web-development mechanisms.
In the first sequence,
we'll introduce inheritance basics.
These concepts are quite similar
to inheritance in other languages with classes,
like Java.
Even if you are familiar with these basics, please watch.
We'll introduce some vocabulary,
and a graphic representation you'll be seeing again
throughout the rest of the course.
This sequence will cover what inheritance is,
and how to use it.
Two classes are represented on the right:
the Rectangle class, with its name, first of all,
its instance variables,
and its methods.
Under it, you see the same thing. The class name, ColoredRectangle,
its instance variables, and its methods.
The vertical arrow here, with its point represented as a blank triangle,
indicates an inheritance relationship.
The ColoredRectangle subclass inherits from the Rectangle class.
Generally, subclasses like ColoredRectangle
refine the behavior and state of their superclass.
A subclass like ColoredRectangle can add state and behavior
to its superclass.
Here, the ColoredRectangle class adds state
in the form of two instance variables, color and border color.
It also adds a method: color.
At some point, we could say that every class
inherits from the Object class.
But that's not quite true.
It is true that every class inherits from ProtoObject class.
The ProtoObject superclass is used for very special cases.
For the purposes of this course in Pharo,
we'll say that all classes inherit from Object.
That will suffice, for our needs.
The ProtoObject class is reserved only for certain specific cases.
You're not likely to need it to learn the basics of Pharo.
Inheritance behaves in two different ways,
depending on whether state or behavior is being inherited.
Inheritance of state is static.
That means that when you create the subclass,
you know exactly what all the instance variables are.
However, inheritance of behavior is dynamic.
During the program's execution, you look at the methods
defined in sub and superclasses.
Concerning instance variables,
inheritance happens during class definition.
When you define the subclass,
you take the instance variables defined in the subclass -
here, they are color and border color,
and you merge them with all the instance variables
defined in the superclass and the line of the superclasses,
all the way up to the Object.
Here, the instance variables of ColoredRectangle
will be with a color and border color.
For behavior, inheritance mechanism changes.
Watch the following sequences
to see exactly how it occurs.
So, on the whole,
behavior inheritance happens at runtime.
In other words, when you send a message to an object,
a method match is searched for in the class hierarchy.
Here, if I send a message to ColoredRectangle...
The "area" message, for example...
it will be searched in the class.
The method will be searched in the ColoredRectangle class.
The "area" method is missing.
So we go up to the superclass.
We find the "area" method. It is selected and executed.
Now, I'll sum up:
For inheritance, the concepts of subclass and superclass
enables the subclass to refine the behavior
of its superclass, and extend it.
The subclass can also extend, or add,
state to its superclass.
Pharo's simple inheritance model stipulates one superclass per class.
The concept of traits enables you multiply inheritance,
but we will not cover that.
Object is at the top of the hierarchy.
So is ProtoObject, but we will not go into that detail.
Inheritance of state is static.
It happens when the subclass is defined.
But inheritance of behavior is dynamic,
happening at runtime,
whenever you send a message to an object.