WEBVTT

00:00:07.560 --> 00:00:08.600 align:middle
Hello, everyone.

00:00:09.120 --> 00:00:12.720 align:middle
Welcome to Sequence 1 of Week 4.

00:00:13.200 --> 00:00:17.960 align:middle
This week, we'll be covering
Pharo inheritance basics,

00:00:18.440 --> 00:00:21.840 align:middle
and look-up
and web-development mechanisms.

00:00:22.480 --> 00:00:24.280 align:middle
In the first sequence,

00:00:24.840 --> 00:00:29.120 align:middle
we'll introduce inheritance basics.

00:00:29.440 --> 00:00:32.360 align:middle
These concepts are quite similar

00:00:32.520 --> 00:00:35.360 align:middle
to inheritance
in other languages with classes,

00:00:35.520 --> 00:00:36.840 align:middle
like Java.

00:00:37.120 --> 00:00:40.560 align:middle
Even if you are familiar
with these basics, please watch.

00:00:40.720 --> 00:00:42.840 align:middle
We'll introduce some vocabulary,

00:00:44.040 --> 00:00:47.680 align:middle
and a graphic representation
you'll be seeing again

00:00:48.120 --> 00:00:50.720 align:middle
throughout the rest of the course.

00:00:51.200 --> 00:00:54.280 align:middle
This sequence will cover
what inheritance is,

00:00:54.480 --> 00:00:55.800 align:middle
and how to use it.

00:00:56.120 --> 00:00:59.040 align:middle
Two classes are represented
on the right:

00:00:59.200 --> 00:01:02.840 align:middle
the Rectangle class,
with its name, first of all,

00:01:03.880 --> 00:01:05.280 align:middle
its instance variables,

00:01:06.120 --> 00:01:07.160 align:middle
and its methods.

00:01:07.560 --> 00:01:11.800 align:middle
Under it, you see the same thing.
The class name, ColoredRectangle,

00:01:12.480 --> 00:01:15.360 align:middle
its instance variables, and its methods.

00:01:16.800 --> 00:01:22.160 align:middle
The vertical arrow here, with
its point represented as a blank triangle,

00:01:22.320 --> 00:01:25.200 align:middle
indicates an inheritance relationship.

00:01:25.360 --> 00:01:29.720 align:middle
The ColoredRectangle subclass
inherits from the Rectangle class.

00:01:29.880 --> 00:01:33.000 align:middle
Generally, subclasses like
ColoredRectangle

00:01:33.160 --> 00:01:37.320 align:middle
refine the behavior and state
of their superclass.

00:01:37.960 --> 00:01:42.400 align:middle
A subclass like ColoredRectangle
can add state and behavior

00:01:42.760 --> 00:01:43.960 align:middle
to its superclass.

00:01:45.120 --> 00:01:49.080 align:middle
Here, the ColoredRectangle class
adds state

00:01:49.320 --> 00:01:53.840 align:middle
in the form of two instance variables,
color and border color.

00:01:55.240 --> 00:01:57.640 align:middle
It also adds a method: color.

00:01:58.120 --> 00:02:01.160 align:middle
At some point, we could say
that every class

00:02:01.520 --> 00:02:04.280 align:middle
inherits from the Object class.

00:02:04.480 --> 00:02:06.160 align:middle
But that's not quite true.

00:02:06.480 --> 00:02:10.240 align:middle
It is true that every class
inherits from ProtoObject class.

00:02:10.520 --> 00:02:15.600 align:middle
The ProtoObject superclass
is used for very special cases.

00:02:15.960 --> 00:02:18.440 align:middle
For the purposes of this course
in Pharo,

00:02:18.600 --> 00:02:21.600 align:middle
we'll say that all classes inherit
from Object.

00:02:21.760 --> 00:02:23.880 align:middle
That will suffice, for our needs.

00:02:24.120 --> 00:02:28.320 align:middle
The ProtoObject class is reserved
only for certain specific cases.

00:02:28.800 --> 00:02:33.440 align:middle
You're not likely to need it
to learn the basics of Pharo.

00:02:34.360 --> 00:02:37.360 align:middle
Inheritance behaves
in two different ways,

00:02:37.520 --> 00:02:41.320 align:middle
depending on whether state
or behavior is being inherited.

00:02:41.840 --> 00:02:43.840 align:middle
Inheritance of state is static.

00:02:44.040 --> 00:02:47.400 align:middle
That means that
when you create the subclass,

00:02:47.760 --> 00:02:53.320 align:middle
you know exactly what
all the instance variables are.

00:02:54.280 --> 00:02:57.240 align:middle
However, inheritance of behavior
is dynamic.

00:02:57.400 --> 00:03:00.840 align:middle
During the program's execution,
you look at the methods

00:03:01.000 --> 00:03:02.920 align:middle
defined in sub and superclasses.

00:03:03.480 --> 00:03:05.240 align:middle
Concerning instance variables,

00:03:05.560 --> 00:03:08.840 align:middle
inheritance happens
during class definition.

00:03:09.240 --> 00:03:11.160 align:middle
When you define the subclass,

00:03:12.800 --> 00:03:16.760 align:middle
you take the instance variables
defined in the subclass -

00:03:16.920 --> 00:03:21.080 align:middle
here, they are color and border color,

00:03:21.600 --> 00:03:26.080 align:middle
and you merge them
with all the instance variables

00:03:26.240 --> 00:03:29.960 align:middle
defined in the superclass
and the line of the superclasses,

00:03:30.120 --> 00:03:31.960 align:middle
all the way up to the Object.

00:03:34.080 --> 00:03:37.000 align:middle
Here, the instance variables
of ColoredRectangle

00:03:37.160 --> 00:03:40.200 align:middle
will be with a color and border color.

00:03:41.400 --> 00:03:44.120 align:middle
For behavior,
inheritance mechanism changes.

00:03:44.560 --> 00:03:47.120 align:middle
Watch the following sequences

00:03:47.760 --> 00:03:50.280 align:middle
to see exactly how it occurs.

00:03:50.560 --> 00:03:51.680 align:middle
So, on the whole,

00:03:52.000 --> 00:03:54.720 align:middle
behavior inheritance happens at runtime.

00:03:55.520 --> 00:03:59.040 align:middle
In other words, when you send
a message to an object,

00:04:00.040 --> 00:04:03.480 align:middle
a method match is searched for
in the class hierarchy.

00:04:04.480 --> 00:04:07.440 align:middle
Here, if I send a message
to ColoredRectangle...

00:04:08.200 --> 00:04:11.280 align:middle
The "area" message, for example...

00:04:11.880 --> 00:04:14.880 align:middle
it will be searched in the class.

00:04:15.040 --> 00:04:18.920 align:middle
The method will be searched
in the ColoredRectangle class.

00:04:19.360 --> 00:04:22.080 align:middle
The "area" method is missing.

00:04:22.240 --> 00:04:24.640 align:middle
So we go up to the superclass.

00:04:25.160 --> 00:04:29.120 align:middle
We find the "area" method.
It is selected and executed.

00:04:30.040 --> 00:04:31.080 align:middle
Now, I'll sum up:

00:04:31.480 --> 00:04:35.280 align:middle
For inheritance, the concepts
of subclass and superclass

00:04:35.600 --> 00:04:38.600 align:middle
enables the subclass
to refine the behavior

00:04:38.760 --> 00:04:41.280 align:middle
of its superclass, and extend it.

00:04:41.560 --> 00:04:45.600 align:middle
The subclass can also extend, or add,

00:04:46.000 --> 00:04:47.960 align:middle
state to its superclass.

00:04:48.400 --> 00:04:52.640 align:middle
Pharo's simple inheritance model
stipulates one superclass per class.

00:04:52.920 --> 00:04:56.440 align:middle
The concept of traits
enables you multiply inheritance,

00:04:56.600 --> 00:04:58.280 align:middle
but we will not cover that.

00:04:58.920 --> 00:05:01.120 align:middle
Object is at the top of the hierarchy.

00:05:01.320 --> 00:05:05.320 align:middle
So is ProtoObject,
but we will not go into that detail.

00:05:05.800 --> 00:05:08.240 align:middle
Inheritance of state is static.

00:05:08.640 --> 00:05:11.960 align:middle
It happens when the subclass is defined.

00:05:12.200 --> 00:05:14.640 align:middle
But inheritance of behavior is dynamic,

00:05:14.920 --> 00:05:16.280 align:middle
happening at runtime,

00:05:16.440 --> 00:05:19.000 align:middle
whenever you send a message
to an object.

