Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
0
1
00:00:00,240 --> 00:00:05,510
Now that we've already talked about abstraction and encapsulation and seen it in action, let's move on
1
2
00:00:05,510 --> 00:00:10,250
to the next pillar of OOP which is inheritance. And inheritance,
2
3
00:00:10,260 --> 00:00:18,120
we're talking about it in programming refers to when a class is able to inherit properties and methods
3
4
00:00:18,450 --> 00:00:20,390
from their parents.
4
5
00:00:20,670 --> 00:00:26,190
So you for example might have inherited your eye color from your parents or you might have inherited
5
6
00:00:26,190 --> 00:00:32,180
certain behaviors like you are a neat freak and you really really need to keep everything super tidy.
6
7
00:00:32,190 --> 00:00:34,890
You might have gotten that from your mom or your dad.
7
8
00:00:34,890 --> 00:00:43,050
Similarly all classes can inherit from a superclass or a parent class and it can inherit properties
8
9
00:00:43,290 --> 00:00:44,970
as well as methods.
9
10
00:00:45,090 --> 00:00:51,690
And this is important because when we're abstracting our program and we're creating lots of different
10
11
00:00:51,690 --> 00:00:57,510
modules lots of different components that all take care of a specific task.
11
12
00:00:57,510 --> 00:01:03,360
It's likely that we will have some duplication in their functionality.
12
13
00:01:03,360 --> 00:01:10,680
So for example our pastry chef and our chef will probably have some similar functionality.
13
14
00:01:10,680 --> 00:01:13,400
They probably both know how to boil some water.
14
15
00:01:13,410 --> 00:01:19,580
They both probably have a method for baking something but they might do it slightly differently.
15
16
00:01:19,740 --> 00:01:26,730
So in order to prevent having to rewrite lots of code when we're abstracting into lots of different
16
17
00:01:26,730 --> 00:01:35,800
classes and separate modules, this is why inheritance is really key when you're creating objects. To demo
17
18
00:01:35,830 --> 00:01:36,390
this,
18
19
00:01:36,430 --> 00:01:42,170
I'm gonna go into a empty DartPad and I'm going to create a new class.
19
20
00:01:42,220 --> 00:01:48,200
Let's say that I create a Car class and my Car class is going to have some properties.
20
21
00:01:48,250 --> 00:01:53,960
Every car probably starts out with a number of seats property right?
21
22
00:01:54,190 --> 00:01:57,640
And we're gonna set it to 5 as the default.
22
23
00:01:57,640 --> 00:02:00,940
Every car I build is going to start out with 5 seats.
23
24
00:02:00,970 --> 00:02:02,590
Seems pretty standard.
24
25
00:02:02,830 --> 00:02:09,550
And then every car is also going to have a drive method because otherwise, why have a car
25
26
00:02:09,550 --> 00:02:15,230
If it doesn't? And when you call the drive method, it simply just turns the wheels.
26
27
00:02:15,400 --> 00:02:25,600
So I'm going to create a print statement that says the 'wheels turn'. And now when I create my car object
27
28
00:02:25,600 --> 00:02:26,890
from my Car class,
28
29
00:02:26,920 --> 00:02:35,680
so let's create a new car. Let's call it myNormalCar because a little bit later on, we're going to obviously
29
30
00:02:36,070 --> 00:02:42,940
make this a lot more souped up. So myNormalCar is just going to be created from the bog standard car
30
31
00:02:42,940 --> 00:02:44,080
class.
31
32
00:02:44,080 --> 00:02:47,470
So when I print myNormalCar
32
33
00:02:51,680 --> 00:03:02,150
.numberOfSeats for example, then you can see that I get 5. And when I say myNormalCar.
33
34
00:03:02,310 --> 00:03:09,810
drive, then it will try and perform the drive functionality and the wheels start turning.
34
35
00:03:10,050 --> 00:03:15,610
That's all pretty standard and we're familiar with how classes objects work in this way now.
35
36
00:03:15,750 --> 00:03:20,490
But what if I wanted to create a different type of car?
36
37
00:03:20,940 --> 00:03:23,290
What if I wanted to create an electric car?
37
38
00:03:23,820 --> 00:03:27,080
Well an electric car is kind of similar to a car right?
38
39
00:03:27,090 --> 00:03:35,310
It probably also has five seats and it probably can drive as well, but it might have some other functionality.
39
40
00:03:35,310 --> 00:03:40,580
Let's go ahead and create a new class and this class is going to be our ElectricCar.
40
41
00:03:41,100 --> 00:03:44,660
But I don't really want to have to type out all of this again.
41
42
00:03:44,670 --> 00:03:47,370
It's just the same as a normal car.
42
43
00:03:47,370 --> 00:03:52,490
But I want to focus my time on explaining how it's different from a normal car.
43
44
00:03:53,010 --> 00:03:57,150
So how can I inherit all of the things I get in car?
44
45
00:03:57,150 --> 00:04:01,500
Well in Dart, the keyword is extends.
45
46
00:04:01,500 --> 00:04:08,180
So my electric car is going to extend my Car class.
46
47
00:04:08,550 --> 00:04:16,180
And what this means is that my ElectricCarr by default, is going to have everything that a car has.
47
48
00:04:16,260 --> 00:04:20,130
So I could go ahead and create a new electric car.
48
49
00:04:21,120 --> 00:04:32,480
Let's call it myTesla and let's create it as a new electric car. And now I can straight up just say
49
50
00:04:32,510 --> 00:04:40,500
myTesla.drive and you'll see that it'll make the wheels turn.
50
51
00:04:40,520 --> 00:04:46,360
Let's go ahead and comment out the previous ones that we had related to our car so we're not getting
51
52
00:04:46,370 --> 00:04:53,810
confused in the console, and you can see this myTesla drive triggers that electric car to turn its wheels
52
53
00:04:54,470 --> 00:05:03,050
because it inherited all of that capability from its parent class, the Car class, and it also has a starting
53
54
00:05:03,080 --> 00:05:04,700
default number of seats.
54
55
00:05:05,420 --> 00:05:13,370
But by inheriting from the Car class, it means I don't have to repeat myself and type out everything
55
56
00:05:13,370 --> 00:05:21,080
that is common to cause an electric cars and I can just focus my time on creating code that is different.
56
57
00:05:21,080 --> 00:05:28,850
For example my electric car might have a battery level and it's going to start out at 100%. But
57
58
00:05:29,330 --> 00:05:36,360
when it goes down, I can recharge my electric car so I can have a method called recharge.
58
59
00:05:36,500 --> 00:05:42,200
And this will reset the battery level to 100%
59
60
00:05:44,930 --> 00:05:51,740
so now not only does my Tesla drive, it also can recharge
60
61
00:05:56,740 --> 00:06:01,140
and I didn't have to repeat the things that a car already has.
61
62
00:06:01,140 --> 00:06:09,230
I've managed to incorporate the properties and methods simply by using that extend keyword.
62
63
00:06:09,230 --> 00:06:14,950
This is how inheritance works and we can see this in our code as well.
63
64
00:06:15,140 --> 00:06:22,520
Every single time that we've created a new stateful widget or stateless widget, we haven't have to
64
65
00:06:22,820 --> 00:06:28,660
create all the properties or the functionality of a stateless or stateful widget.
65
66
00:06:28,730 --> 00:06:36,530
We've simply been able to tap into it by creating a class that extends the parent class a stateful
66
67
00:06:36,620 --> 00:06:37,950
or stateless widget.
67
68
00:06:38,090 --> 00:06:44,600
And if you hold down COMMAND or CONTROL on Windows and you click on that class, you can see that it has
68
69
00:06:44,660 --> 00:06:52,340
various properties and methods that we've been able to simply use because we're extending it. We're
69
70
00:06:52,340 --> 00:06:59,090
inheriting from it and all we have to do is to describe all the things are different from the parent
70
71
00:06:59,090 --> 00:07:06,770
class and focus on building out our custom functionality rather than on the basic boring things such
71
72
00:07:06,770 --> 00:07:09,480
as how do we draw a widget on screen?
72
73
00:07:09,530 --> 00:07:11,240
How do we give it a different color?
73
74
00:07:11,240 --> 00:07:13,430
How do we change its shape and size?
74
75
00:07:13,430 --> 00:07:15,500
How do we actually move the pixels around?
75
76
00:07:15,770 --> 00:07:24,140
That's all sorted out for you by the Flutter SDK and all of that capability are packaged into these
76
77
00:07:24,140 --> 00:07:30,550
parent widgets that we're able to inherit from. Now in the next lesson,
77
78
00:07:30,580 --> 00:07:34,750
I want to talk a little bit about the last pillar which is polymorphism.
78
79
00:07:34,750 --> 00:07:38,010
So for all of that and more, I'll see you on the next lesson.
9044
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.