Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
0
1
00:00:00,450 --> 00:00:06,890
So we're on to our final pillar and that's polymorphism or changing shapes.
1
2
00:00:06,930 --> 00:00:08,310
How does this work?
2
3
00:00:08,310 --> 00:00:14,320
Let's say that we're abstracting our program and we're creating a separate module for separate roles.
3
4
00:00:14,580 --> 00:00:18,870
And we're inheriting a lot of the behavior from parent classes.
4
5
00:00:18,870 --> 00:00:25,420
But when we're doing a lot of inheritance, we know that we get basically a copy of what the parent class
5
6
00:00:25,420 --> 00:00:26,590
is able to do.
6
7
00:00:26,610 --> 00:00:32,640
So our electric car can drive and our electric car has the same properties. But we don't really want
7
8
00:00:32,640 --> 00:00:37,830
to end up with just a whole bunch of clones in our program. That isn't great
8
9
00:00:37,830 --> 00:00:38,100
right?
9
10
00:00:38,100 --> 00:00:42,120
We want to be able to define customer behavior as well.
10
11
00:00:42,210 --> 00:00:45,900
So in the case of our chef and our pastry chef,
11
12
00:00:45,900 --> 00:00:49,290
well, there's probably similarities between those two roles.
12
13
00:00:49,320 --> 00:00:54,660
They can probably both bake something in the oven and they can probably both make some sauce in a saucepan.
13
14
00:00:55,110 --> 00:00:59,950
But they might go about those behaviors a little bit differently.
14
15
00:01:00,150 --> 00:01:05,730
So maybe the pastry chef has to be a little bit more delicate with the things that he's working with,
15
16
00:01:06,240 --> 00:01:07,930
and the chef maybe,
16
17
00:01:07,950 --> 00:01:12,330
well she has to do things a little bit quicker because it's complete mayhem in the kitchen.
17
18
00:01:12,330 --> 00:01:18,060
But the point is that when you are inheriting from a parent class, there are probably things that will
18
19
00:01:18,060 --> 00:01:23,100
save yourself time that you don't have to recreate and you can simply inherit.
19
20
00:01:23,100 --> 00:01:29,130
But some of those behaviors that you inheriting, some of the methods, you might want to customize it for
20
21
00:01:29,130 --> 00:01:31,220
your own needs.
21
22
00:01:31,290 --> 00:01:33,360
There's two ways that you can do this.
22
23
00:01:33,390 --> 00:01:44,820
Continuing our car analogy, we might have simply a LevitatingCar, and this LevitatingCar is going to
23
24
00:01:45,240 --> 00:01:47,430
inherit from our Car class.
24
25
00:01:47,430 --> 00:01:54,090
So we're going to write extends the Car class, and this is because a levitating car probably also has 5
25
26
00:01:54,090 --> 00:01:59,430
seats, then it probably also looks pretty much the same as a car. It might share the same properties
26
27
00:01:59,920 --> 00:02:05,540
but a levitating car doesn't have wheels, at least in my mind.
27
28
00:02:05,670 --> 00:02:13,440
And instead, if we were to implement the drive functionality of a levitating car, it would probably be
28
29
00:02:13,560 --> 00:02:20,550
a little bit different. Because we're already inheriting from the Car class that has a drive method, if
29
30
00:02:20,550 --> 00:02:28,800
we want to use the same method, we have to override it and to do that we use the @ sign to specify
30
31
00:02:28,800 --> 00:02:35,820
that there's a very important word coming up, and the word is override. And then on the next line, we can
31
32
00:02:35,820 --> 00:02:45,870
create our own version of drive so we can write void drive and it looks exactly the same as before. But
32
33
00:02:45,930 --> 00:02:53,460
now, it's our own version of the drive method and our levitating car instead of moving wheels,
33
34
00:02:53,460 --> 00:02:59,110
it's going to simply glide forwards, very classy.
34
35
00:02:59,130 --> 00:03:02,910
So now when we create our levitating car,
35
36
00:03:06,040 --> 00:03:13,920
and we call upon our levitating car, our MagLev to drive, then you can see that it should now glide forwards
36
37
00:03:14,100 --> 00:03:22,580
instead of using its parents implementation of drive. So even though I inherit from my parents a number
37
38
00:03:22,580 --> 00:03:28,370
of traits and a number of behaviors, there's also things that I do differently from them or I'd like
38
39
00:03:28,370 --> 00:03:29,990
to think I do differently.
39
40
00:03:29,990 --> 00:03:35,510
For example the way I use a computer, I might get to use a computer to program it whereas my parents
40
41
00:03:35,540 --> 00:03:37,910
might just use it to go on Facebook.
41
42
00:03:38,000 --> 00:03:48,740
But the point is we are now able to override a parent behavior and provide our own custom version of
42
43
00:03:48,740 --> 00:03:52,890
it but we can actually go even further than that.
43
44
00:03:53,180 --> 00:04:00,400
We can actually take some of the good parts from our parent class, but simply just add to it.
44
45
00:04:00,410 --> 00:04:01,720
Let me show you what I mean.
45
46
00:04:01,790 --> 00:04:06,520
Let's build a new class and this is going to be a self driving car.
46
47
00:04:06,740 --> 00:04:08,110
And of course it's a car.
47
48
00:04:08,120 --> 00:04:11,960
So it's going to extend the car class.
48
49
00:04:12,080 --> 00:04:18,780
It's going to have a drive method and it's going to have 5 seats. So, easy. That was done.
49
50
00:04:18,860 --> 00:04:25,840
But in addition to being able to drive and make the wheels turn,
50
51
00:04:26,420 --> 00:04:34,520
I also want my self-driving car to be able to steer the car as well. Instead of simply just getting the
51
52
00:04:34,520 --> 00:04:35,890
wheels to turn,
52
53
00:04:35,960 --> 00:04:40,980
I'm going to get my self-driving car to drive me towards a destination.
53
54
00:04:41,150 --> 00:04:50,690
So I'm going to create a new property called destination. And this destination is going to be set when
54
55
00:04:50,690 --> 00:04:53,750
I create a new object from the self-driving car.
55
56
00:04:54,140 --> 00:05:02,300
So I'm going to define a custom constructor, so self-driving car, when we create it,
56
57
00:05:02,360 --> 00:05:10,130
we have to provide a destination. And we'll call that userSetDestination.
57
58
00:05:10,130 --> 00:05:17,900
And then once we create our self-driving car, we'll set the destination property to equal the user set
58
59
00:05:17,900 --> 00:05:24,250
destination that was provided when we created our new object.
59
60
00:05:24,260 --> 00:05:34,010
Now inside this self-driving car, when we decide to drive it, we kind of want to inherit the functionality
60
61
00:05:34,100 --> 00:05:37,240
of driving which is wheels moving,
61
62
00:05:37,280 --> 00:05:40,200
and as a consequence the car moving.
62
63
00:05:40,340 --> 00:05:48,450
But we want to add to it as well. We can again override our parent method code drive.
63
64
00:05:48,470 --> 00:05:50,860
So it has to look exactly the same.
64
65
00:05:50,960 --> 00:05:54,890
It's also void drive empty parentheses.
65
66
00:05:54,890 --> 00:06:02,390
And then we're going to trigger the parent behavior by calling super, which stands for the superclass
66
67
00:06:02,420 --> 00:06:06,080
or the parent.drive.
67
68
00:06:06,110 --> 00:06:11,720
This is going to carry out the behavior of the superclass' drive method.
68
69
00:06:11,720 --> 00:06:21,110
But then afterwards, we're going to do our own thing. We're going to say that the steering is going to
69
70
00:06:21,170 --> 00:06:25,370
steer us towards the destination.
70
71
00:06:27,860 --> 00:06:36,050
Here's how it would look when we actually call this method in our main. So we could create our new self
71
72
00:06:36,140 --> 00:06:50,410
driving car, we'll call it myWaymo. And it's gonna be equal to a new self driving car. And when I create
72
73
00:06:50,500 --> 00:06:56,370
myWaymo as a new self-driving car, I have to provide a destination.
73
74
00:06:56,380 --> 00:06:58,720
So let's drive it towards 1 Hacker way.
74
75
00:07:02,590 --> 00:07:09,610
And now when I say myWaymo.drive calling that drive method,
75
76
00:07:09,610 --> 00:07:13,660
then when I hit run, you'll see two things happen.
76
77
00:07:13,690 --> 00:07:23,230
One is that it'll carry out the parents version of drive, which is simply to print 'wheels turn', but it'll
77
78
00:07:23,230 --> 00:07:29,830
also do its own thing which is steering towards the destination.
78
79
00:07:29,830 --> 00:07:35,490
And this all comes from just calling that one drive method.
79
80
00:07:35,530 --> 00:07:37,380
This is polymorphism in action.
80
81
00:07:37,390 --> 00:07:43,030
We're able to inherit from our parents, but we're able to also improve on it.
81
82
00:07:43,030 --> 00:07:52,240
We're able to change it be a little bit different by overriding their methods. And if we come back to
82
83
00:07:52,330 --> 00:07:58,400
our Flutter code, you can see that we're doing that every time we're calling the build method.
83
84
00:07:58,540 --> 00:08:07,990
So a stateless widget already has an implementation for the build method, but in our code, we override
84
85
00:08:07,990 --> 00:08:12,940
it and we provide what it is that we want to do instead.
85
86
00:08:13,120 --> 00:08:19,600
And this way, even though we're inheriting, we're able to be a little bit different and customize what
86
87
00:08:19,600 --> 00:08:22,670
our stateless or our stateful widgets do.
87
88
00:08:22,810 --> 00:08:29,860
So getting the best parts, but then also doing custom things using these inherited classes.
9986
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.