Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,000 --> 00:00:07,160
Now in this section we want to talk about inheritance and we're referring to inheritance
2
00:00:07,160 --> 00:00:09,280
as a software design concept.
3
00:00:09,280 --> 00:00:11,080
It's taken from the real world.
4
00:00:11,080 --> 00:00:15,640
So you inherit certain characteristics from your parents but you also have unique qualities
5
00:00:15,640 --> 00:00:16,720
of your own.
6
00:00:16,720 --> 00:00:21,440
In the same way in software we can model an inheritance relationship between classes.
7
00:00:21,440 --> 00:00:26,120
So we might have some child classes that inherit behavior from parent classes but they also
8
00:00:26,120 --> 00:00:28,320
might have their own unique behavior.
9
00:00:28,320 --> 00:00:31,160
So a good example of this is like a 3D movie.
10
00:00:31,160 --> 00:00:38,200
It has similar characteristics to a regular movie but it also has unique qualities.
11
00:00:38,200 --> 00:00:40,800
You also have to wear special glasses at a 3D movie.
12
00:00:40,800 --> 00:00:41,800
That's true.
13
00:00:41,800 --> 00:00:44,720
Now to create a 3D movie we don't want to copy all the code that we already have in
14
00:00:44,720 --> 00:00:46,160
a movie class.
15
00:00:46,160 --> 00:00:49,560
Rather we just want to extend the functionality that we already have.
16
00:00:49,560 --> 00:00:51,700
So we've got the code in one place.
17
00:00:51,700 --> 00:00:53,320
So let's have a look at that.
18
00:00:53,320 --> 00:00:57,640
So we have our movie class here and it has all the basic behavior of a movie but we want
19
00:00:57,640 --> 00:00:58,640
a 3D movie.
20
00:00:58,640 --> 00:00:59,640
So I'm going to create a new file.
21
00:00:59,640 --> 00:01:05,800
I'm going to call the file movie3d.rb because class names in Ruby can't start with a number
22
00:01:05,800 --> 00:01:10,480
so I'm going to call it movie3d and then we'll just require our movie because we're going
23
00:01:10,480 --> 00:01:13,400
to base our movie3d class on it.
24
00:01:13,400 --> 00:01:19,840
I'm going to say class movie3d like that and then to inherit from movie we just use the
25
00:01:19,840 --> 00:01:23,400
less than sign and we give it the class name movie.
26
00:01:23,400 --> 00:01:27,360
So that's all we need to do to set up an inheritance relationship between these two classes.
27
00:01:27,360 --> 00:01:33,720
Then I can just go ahead and create a 3D movie movie3d call new.
28
00:01:33,720 --> 00:01:38,280
We can pass in the same attributes here or set up the state the same way we do a regular
29
00:01:38,280 --> 00:01:39,280
movie.
30
00:01:39,280 --> 00:01:42,920
I'm going to call this movie Glee because Glee is a 3D movie and it's going to have
31
00:01:42,920 --> 00:01:51,640
a rank of five and then I can just turn around and print out movie3d has a title and movie3d
32
00:01:51,640 --> 00:01:53,120
has a rank.
33
00:01:53,120 --> 00:01:55,560
So if we run that we get Glee in five.
34
00:01:55,560 --> 00:01:59,880
So we see that we've inherited the attributes of our parent class movie.
35
00:01:59,880 --> 00:02:02,480
We didn't have to do anything special in this class.
36
00:02:02,480 --> 00:02:09,360
We can also turn around and call movie3d.thumbsup for example and then we'll just print out
37
00:02:09,360 --> 00:02:10,360
its rank again.
38
00:02:10,360 --> 00:02:16,200
In fact we'll print out the whole movie3d and if we run that so we start off with five
39
00:02:16,200 --> 00:02:18,160
we get thumbs up to six.
40
00:02:18,160 --> 00:02:22,240
We print out the rank again and notice that the 2S method is being called on the movie
41
00:02:22,240 --> 00:02:23,240
as well.
42
00:02:23,240 --> 00:02:26,520
So it's acting just like a regular movie.
43
00:02:26,520 --> 00:02:30,780
So I want to quickly show you how you can use reflection to look at the class hierarchy
44
00:02:30,780 --> 00:02:31,780
involved here.
45
00:02:31,780 --> 00:02:36,080
Out on the command line I'm just going to load up IRB and then I'm actually going to
46
00:02:36,080 --> 00:02:39,440
I can load that file movie3d.rb.
47
00:02:39,440 --> 00:02:43,560
So this is just going to load in the file that we just created which will define our
48
00:02:43,560 --> 00:02:47,320
movie3d class and you see that we get the output because we've got that little example
49
00:02:47,320 --> 00:02:48,720
code at the bottom.
50
00:02:48,720 --> 00:02:53,840
And then we can look at movie3d what is your superclass?
51
00:02:53,840 --> 00:02:57,160
Well its superclass or its parent is the movie class.
52
00:02:57,160 --> 00:03:02,360
Another one we can try is movie3d.ask it for its entire ancestor chain by calling the
53
00:03:02,360 --> 00:03:04,180
ancestors method.
54
00:03:04,180 --> 00:03:06,240
So we've got movie3d.
55
00:03:06,240 --> 00:03:08,160
Its direct parent is movie.
56
00:03:08,160 --> 00:03:12,440
Movie's parent is object which is a class built into Ruby.
57
00:03:12,440 --> 00:03:15,600
Kernel is actually something that gets mixed in so we can ignore this now.
58
00:03:15,600 --> 00:03:19,920
Also make more sense in the next section and then at the very top is this class called
59
00:03:19,920 --> 00:03:21,920
basic object.
60
00:03:21,920 --> 00:03:23,959
Well let's look at this visually.
61
00:03:23,959 --> 00:03:28,959
If we ask our movie class for its object ID it will look up the inheritance chain to
62
00:03:28,959 --> 00:03:32,540
object which defines object ID.
63
00:03:32,540 --> 00:03:37,560
Object also has a 2S method which is why every object can call 2S.
64
00:03:37,560 --> 00:03:41,820
Now however in our case we've overridden 2S in our movie class.
65
00:03:41,820 --> 00:03:47,280
So when we call 2S on movie it uses our custom version of 2S.
66
00:03:47,280 --> 00:03:52,299
When we call thumbs up on a movie it invokes that method in the movie class.
67
00:03:52,299 --> 00:03:57,459
Now we introduce our 3D movie class and when we call thumbs up on it Ruby looks up the
68
00:03:57,459 --> 00:04:01,680
inheritance chain until it finds the method under movie.
69
00:04:01,680 --> 00:04:02,760
So here's the take away.
70
00:04:02,760 --> 00:04:07,519
When you call a method on an object Ruby first tries to find that method in the object's
71
00:04:07,519 --> 00:04:08,519
class.
72
00:04:08,520 --> 00:04:13,560
When it isn't found Ruby looks in the object superclass and its superclass and so on all
73
00:04:13,560 --> 00:04:15,900
the way up to the top of the chain.
74
00:04:15,900 --> 00:04:20,560
So we've seen how subclasses inherit behavior and attributes from their parents.
75
00:04:20,560 --> 00:04:23,920
Subclasses can also define their own unique behavior that won't be available in their
76
00:04:23,920 --> 00:04:24,920
parents.
77
00:04:24,920 --> 00:04:26,120
Let's try that.
78
00:04:26,120 --> 00:04:29,000
Back in our movie 3D class here we're going to add some new behavior.
79
00:04:29,000 --> 00:04:31,120
It's just going to be a new method.
80
00:04:31,120 --> 00:04:36,520
Movie 3D movies we're going to have a method called show effect and when we call the show
81
00:04:36,520 --> 00:04:40,640
effect method what we want to do is print out wow.
82
00:04:40,640 --> 00:04:43,000
Actually we want to print out wow a whole bunch of times.
83
00:04:43,000 --> 00:04:47,280
The number of times we're going to do it is going to be defined by this wow factor variable.
84
00:04:47,280 --> 00:04:53,320
We're going to set it to 10 and then we're going to print out wow and then we can actually
85
00:04:53,320 --> 00:04:55,919
multiply a string by a number and it'll print it that many times.
86
00:04:55,919 --> 00:04:58,840
So wow times the wow factor.
87
00:04:58,840 --> 00:05:07,700
So now down in here when we're all done we'll just call movie3D.showEffect and it prints
88
00:05:07,700 --> 00:05:09,960
out wow 10 times.
89
00:05:09,960 --> 00:05:10,960
Wow.
90
00:05:10,960 --> 00:05:11,960
Wow.
91
00:05:11,960 --> 00:05:15,719
Okay, so what if we have a regular movie?
92
00:05:15,719 --> 00:05:19,880
Well if I just have a movie variable I have our old friend Goonies here and I could try
93
00:05:19,880 --> 00:05:22,760
to call movie.showEffect.
94
00:05:22,760 --> 00:05:24,000
What happens here?
95
00:05:24,000 --> 00:05:28,419
Well when we run it undefined method show effect for Goonies because movie the parent
96
00:05:28,420 --> 00:05:31,120
class in this case doesn't have a show effect method.
97
00:05:31,120 --> 00:05:34,600
We can only call that on 3D movies.
98
00:05:34,600 --> 00:05:35,960
No wow for Goonies.
99
00:05:35,960 --> 00:05:39,480
Yeah, a big no wow.
100
00:05:39,480 --> 00:05:43,840
Finally subclasses can specialize behavior by overriding methods that are already defined
101
00:05:43,840 --> 00:05:45,400
in their parent class.
102
00:05:45,400 --> 00:05:50,280
Let's do that here up in our 3D movie we're going to define a thumbs up method which we
103
00:05:50,280 --> 00:05:54,400
know is defined in the movie class because we put it up there and we're going to override
104
00:05:54,400 --> 00:05:59,440
that we're going to define a wow factor local variable in here again and what we're going
105
00:05:59,440 --> 00:06:03,840
to do is we're going to increase our rank by one.
106
00:06:03,840 --> 00:06:08,440
Actually we're going to increase our rank by one times the wow factor.
107
00:06:08,440 --> 00:06:12,440
Right because 3D movies regardless of plot should increase in rank.
108
00:06:12,440 --> 00:06:15,400
Yeah, they're going to go up by tens here right?
109
00:06:15,400 --> 00:06:20,520
So we've overridden that method and then down here I'm going to take off the movie we had
110
00:06:20,520 --> 00:06:21,620
before.
111
00:06:21,620 --> 00:06:24,640
So now when we call this when we call thumbs up we run it.
112
00:06:24,640 --> 00:06:29,000
Glee starts out at five that's the initial rank but then goes up to 15 when we thumbs
113
00:06:29,000 --> 00:06:30,000
up it.
114
00:06:30,000 --> 00:06:35,600
So we've overridden the behavior of the thumbs up method that was defined in our parent class.
115
00:06:35,600 --> 00:06:37,520
Thumbs down however is unchanged.
116
00:06:37,520 --> 00:06:40,360
It's still going to decrement the rank by one.
117
00:06:40,360 --> 00:06:45,680
Also worth noting here is that we can access this instance variable at rank which is defined
118
00:06:45,680 --> 00:06:47,840
up in our movie class.
119
00:06:47,840 --> 00:06:52,039
Classes have access to their parent classes instance variables.
120
00:06:52,039 --> 00:06:54,440
Now let's retrace the method look up here.
121
00:06:54,440 --> 00:06:59,919
When we add the method show effect to our 3D movie and then call it there's no inheritance.
122
00:06:59,919 --> 00:07:02,739
The behavior is defined right in the class.
123
00:07:02,739 --> 00:07:08,039
Now when we define thumbs up in our movie 3D class then the old inheritance falls away
124
00:07:08,039 --> 00:07:10,159
and the new method gets called.
125
00:07:10,159 --> 00:07:14,320
In other words the new method overrides the method in the parent class.
126
00:07:14,320 --> 00:07:18,960
Now I want to show you how we can call methods in the parent class from the child class using
127
00:07:18,960 --> 00:07:20,920
something called super.
128
00:07:20,920 --> 00:07:26,300
Alright back in our movie 3D class here we've got this local variable called wow factor.
129
00:07:26,300 --> 00:07:30,920
We'd like to remove this duplication we really want this to be part of the state of a 3D
130
00:07:30,920 --> 00:07:31,920
movie.
131
00:07:31,920 --> 00:07:35,520
So we know that to do that we're going to have to have an initialize method.
132
00:07:35,520 --> 00:07:41,240
Initialize and we'll pass in the initial wow factor and we want to save that in the instance
133
00:07:41,240 --> 00:07:47,080
variable wow factor like that.
134
00:07:47,080 --> 00:07:51,040
But if we do this we're going to override the initialize that's in our parent class.
135
00:07:51,040 --> 00:07:57,040
So we really need to also take in the title and the rank that a regular movie needs.
136
00:07:57,040 --> 00:08:01,480
Now inside of here we could assign an instance variable title and instance variable rank
137
00:08:01,480 --> 00:08:03,880
but our parent class already does that.
138
00:08:03,880 --> 00:08:09,080
We'd rather just let it set up a regular movie and then we just set up the wow factor.
139
00:08:09,080 --> 00:08:13,620
So the way we can do that is by calling super and then just pass the title and the rank.
140
00:08:13,620 --> 00:08:17,919
This will cause the initialize method to be run in the movie class.
141
00:08:17,919 --> 00:08:23,479
Okay, so then when we create a new movie 3D instead of just passing in the title and the
142
00:08:23,479 --> 00:08:26,479
rank there I can also pass in the wow factor.
143
00:08:26,479 --> 00:08:29,700
Let's say the wow factor of Glee was 10.
144
00:08:29,700 --> 00:08:34,740
So when I call new here what's going to happen is it's going to call initialize in our class
145
00:08:34,740 --> 00:08:37,480
movie 3D passing in the title and the rank.
146
00:08:37,480 --> 00:08:41,760
It's going to let the movie set up the title and the rank but then we're going to set the
147
00:08:41,760 --> 00:08:46,800
wow factor because this is part of our specialized state of a 3D movie.
148
00:08:46,800 --> 00:08:48,400
That seems to be initializing right.
149
00:08:48,400 --> 00:08:52,440
So we can go ahead now and instead of having these local variables we'll just change it
150
00:08:52,440 --> 00:08:55,240
over so we've got this being an instance variable.
151
00:08:55,240 --> 00:08:59,260
This is an instance variable just like that and that works as well.
152
00:08:59,260 --> 00:09:00,760
So what else can we do with super?
153
00:09:00,760 --> 00:09:06,040
Well we've got this thumbs up method right now and what we're doing is we're taking the
154
00:09:06,040 --> 00:09:09,240
wow factor times one and then incrementing the rank.
155
00:09:09,240 --> 00:09:12,360
And in our movie class we already have some default behavior.
156
00:09:12,360 --> 00:09:14,599
We're incrementing the rank by one.
157
00:09:14,599 --> 00:09:19,339
So instead of just completely overriding thumbs up here what we'd rather do is do something
158
00:09:19,339 --> 00:09:25,740
like take the wow factor that number of times we'll just call super.
159
00:09:25,740 --> 00:09:30,760
So what this is going to do is it's going to call the thumbs up method in the movie
160
00:09:30,760 --> 00:09:33,640
class however many times we have as a wow factor.
161
00:09:33,640 --> 00:09:36,240
In this case it's going to call it ten times.
162
00:09:36,240 --> 00:09:40,640
And the reason we do it this way is the logic might change in the movie class and we wouldn't
163
00:09:40,640 --> 00:09:42,760
want to duplicate that change down here.
164
00:09:42,760 --> 00:09:44,620
We just let it do whatever it's going to do.
165
00:09:44,620 --> 00:09:47,460
In this case it's just going to increase the rank by one.
166
00:09:47,460 --> 00:09:52,120
So instead we'll let the parent class handle all the updating of the rank by calling super
167
00:09:52,120 --> 00:09:55,000
one time for each wow factor.
168
00:09:55,000 --> 00:09:57,199
Let's change the wow factor to 20.
169
00:09:57,199 --> 00:09:58,680
Sure.
170
00:09:58,680 --> 00:09:59,680
So it starts at five.
171
00:09:59,680 --> 00:10:03,160
The wow factor is going to be 20 in this case.
172
00:10:03,160 --> 00:10:04,160
We run it.
173
00:10:04,160 --> 00:10:06,280
I know this output is a little confusing.
174
00:10:06,280 --> 00:10:10,719
We have a rank of five but then there's this 20 in here and then 25 after that.
175
00:10:10,719 --> 00:10:12,360
So where's that coming from?
176
00:10:12,360 --> 00:10:16,360
Well if we look down our printout we're printing out the rank but then we're calling thumbs
177
00:10:16,360 --> 00:10:18,240
up and there's a put S before it.
178
00:10:18,240 --> 00:10:20,819
So whatever thumbs up returns is going to get printed.
179
00:10:20,819 --> 00:10:22,240
So let's just go ahead and take that out.
180
00:10:22,240 --> 00:10:26,240
So we're going to start with a rank of five and a wow factor of 20.
181
00:10:26,240 --> 00:10:27,240
Let's run it again.
182
00:10:27,240 --> 00:10:33,600
So we start with five and we end up with 25 because we called the thumbs up method in
183
00:10:33,600 --> 00:10:38,040
the parent class 20 times which gave us 20.
184
00:10:38,040 --> 00:10:41,960
Add the initial rank of five and we end up with 25.
185
00:10:41,960 --> 00:10:46,440
So now that we've set up an inheritance relationship between a 3D movie and a movie we really have
186
00:10:46,440 --> 00:10:48,040
this is a relationship.
187
00:10:48,040 --> 00:10:50,920
A 3D movie is a kind of movie.
188
00:10:50,920 --> 00:10:55,620
So therefore we should be able to use a 3D movie in our program anywhere where we expect
189
00:10:55,620 --> 00:10:56,800
to use a movie.
190
00:10:56,800 --> 00:10:59,579
This is sometimes called polymorphism.
191
00:10:59,579 --> 00:11:04,959
So back over in Flix.rb and we're loading up our movies via this load method we could
192
00:11:04,959 --> 00:11:08,920
also go ahead and add a 3D movie into our playlist.
193
00:11:08,920 --> 00:11:15,000
So first I'm going to make sure I require movie 3D up there and then down here I can
194
00:11:15,000 --> 00:11:21,599
say movie 3D equals movie 3D.new.
195
00:11:21,599 --> 00:11:26,420
We'll use Glee again and it'll start with initial rank of five and it'll have a wow
196
00:11:26,420 --> 00:11:27,920
factor of 20.
197
00:11:27,920 --> 00:11:33,719
And then we can take our playlist and we can add that movie because a 3D movie is a movie
198
00:11:33,719 --> 00:11:36,160
the playlist will just happily play that.
199
00:11:36,160 --> 00:11:40,560
It's got a thumbs up method and a thumbs down method and then if we come back out to the
200
00:11:40,560 --> 00:11:46,959
command line we can just run Ruby Flix.rb and we've got that little printed output that
201
00:11:46,959 --> 00:11:50,660
we have at the bottom of our movie 3D but we see a number of viewings.
202
00:11:50,660 --> 00:11:53,280
We can do that and we see that Glee is in the mix.
203
00:11:53,280 --> 00:11:58,319
It got a thumbs up, it consumed some pretzel carbs and we see that we've got all of our
204
00:11:58,319 --> 00:11:59,319
snacks in there.
205
00:11:59,319 --> 00:12:01,760
So it's playing with all the other movies.
206
00:12:01,760 --> 00:12:02,760
Yay!
207
00:12:02,760 --> 00:12:06,000
And with that you're ready for my favorite exercise.
208
00:12:06,000 --> 00:12:10,439
So in the same way that we just created a 3D movie you're going to create two new player
209
00:12:10,439 --> 00:12:11,439
types.
210
00:12:11,439 --> 00:12:15,000
One's going to be a little clumsy and one's going to be a little crazy.
211
00:12:15,000 --> 00:12:16,000
You'll see what I mean.
212
00:12:16,000 --> 00:12:23,320
And in the next section we'll look at another way to share code across classes called mixins.
19568
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.