All language subtitles for 10. Abstraction in Action

af Afrikaans
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
tl Filipino
fi Finnish
fr French Download
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 0 1 00:00:00,300 --> 00:00:06,390 In the last lesson, we've seen how we can create our classes and our objects using Dart. 1 2 00:00:06,390 --> 00:00:09,190 But why do we actually need them anyways? 2 3 00:00:09,210 --> 00:00:11,630 What purpose do they serve? 3 4 00:00:11,640 --> 00:00:18,600 Well in order to understand this, we first have to understand how computers actually work. Behind the 4 5 00:00:18,600 --> 00:00:22,790 metal shell and the pretty keyboard and the nice screen, 5 6 00:00:23,040 --> 00:00:31,680 a computer is just composed of a large number of switches. And these switches can be in two states, on 6 7 00:00:31,980 --> 00:00:37,050 or off. And it's through switching on and off many different switches 7 8 00:00:37,110 --> 00:00:40,470 we end up telling the computer what it should do. 8 9 00:00:40,620 --> 00:00:47,520 Now if you had to sit there and write all of these ones and zeros in order to program your computer, 9 10 00:00:47,850 --> 00:00:52,840 you'd be there for years and in fact this is how people used to code way way back. 10 11 00:00:52,950 --> 00:01:01,190 But then we thought that this is a bit painful because in order to just represent the letter A, 11 12 00:01:01,350 --> 00:01:10,140 I have to write 01000001. And that is very painful and very time consuming. 12 13 00:01:10,340 --> 00:01:13,760 This is what we would say is machine code. 13 14 00:01:13,760 --> 00:01:21,980 So we came up with all sorts of ways of making it easier to write programs that sound and look more 14 15 00:01:21,980 --> 00:01:28,130 like English so that we can actually interact with the computer without having to get down to the machine 15 16 00:01:28,130 --> 00:01:30,880 level and start writing ones and zeros. 16 17 00:01:31,100 --> 00:01:36,290 In the early days, a lot of the programming languages were what we would call procedural. 17 18 00:01:36,290 --> 00:01:44,140 They would go from top to bottom/ So do this, then this then this then this then this, until forever basically. 18 19 00:01:44,960 --> 00:01:53,390 And this is kind of easy to understand. But as your code gets more and more complex, then it gets a bit 19 20 00:01:53,390 --> 00:01:58,390 hairy cause it's hard to know when something goes wrong, 20 21 00:01:58,490 --> 00:02:00,010 what exactly went wrong? 21 22 00:02:00,020 --> 00:02:06,050 Because you have to follow the logic from top to bottom and you have to really untangle it and you can 22 23 00:02:06,050 --> 00:02:08,780 really shoot yourself in the foot quite easily. 23 24 00:02:08,780 --> 00:02:13,620 So in a lot of places, complexity is always the enemy. 24 25 00:02:13,640 --> 00:02:21,620 As humans we have pretty capable brains but we can only hold so much information in it at one time. The 25 26 00:02:21,620 --> 00:02:28,670 more complex a program gets, the more likely it is that it will crash and it will have problems and it 26 27 00:02:28,670 --> 00:02:31,520 won't perform very well. 27 28 00:02:31,520 --> 00:02:38,500 So along came this concept of Object Oriented Programming, and a lot of people really loved the concept. 28 29 00:02:38,600 --> 00:02:45,800 So languages that became really big such as Oracles, Java or Apple Swift or in fact Dart,which we're working 29 30 00:02:45,800 --> 00:02:50,110 with, are all Object Oriented Programming languages. 30 31 00:02:50,120 --> 00:02:51,500 So what does that mean? 31 32 00:02:52,040 --> 00:02:58,610 Well it means that we're working with objects to do everything that our app or our program needs to 32 33 00:02:58,610 --> 00:02:59,500 do. 33 34 00:02:59,570 --> 00:03:03,890 And for those guys who are looking for a job as a software engineer, one of the most common questions 34 35 00:03:03,890 --> 00:03:09,450 you get asked is 'What are the four pillars of Object Oriented Programming?' 35 36 00:03:09,500 --> 00:03:13,910 What is Object Oriented Programming all about? And the four pillars 36 37 00:03:13,910 --> 00:03:19,700 you'll very often hear people reel off are abstraction, encapsulation, inheritance and polymorphism. 37 38 00:03:19,820 --> 00:03:27,310 Now it's a mouthful but very few people actually fully understand what these big words actually mean. 38 39 00:03:27,320 --> 00:03:35,240 So I want to break it down and show you in the context of our app, why OOP is so great, and how does 39 40 00:03:35,240 --> 00:03:36,660 it apply to Dart. 40 41 00:03:36,950 --> 00:03:39,490 One of the first pillars is abstraction. 41 42 00:03:39,650 --> 00:03:46,120 How can we make something that's complex into smaller pieces to make it more abstract? 42 43 00:03:46,160 --> 00:03:49,830 Now if you've taken my courses before, you know how much I love food. 43 44 00:03:49,940 --> 00:03:53,990 So in order to understand abstraction, we're going to talk about, you guessed it, food. 44 45 00:03:53,990 --> 00:04:01,130 One of my favorite restaurants in Japan are these really tiny kind of hole-in-the-wall places where 45 46 00:04:01,130 --> 00:04:09,230 there's only maybe four or five seats. But there's also just one guy who is the waiter, the chef, the cleaning 46 47 00:04:09,230 --> 00:04:16,160 staff and he just does everything from behind the bar counter. When you make an order, you do it on a 47 48 00:04:16,160 --> 00:04:21,380 vending machine and you give him the order. And then he'll cook you order he'll put it on the table for 48 49 00:04:21,380 --> 00:04:22,840 you because it's so easy to reach. 49 50 00:04:22,970 --> 00:04:26,190 And then once you're done he'll take your bowl and he'll put it in the dishwasher. 50 51 00:04:26,210 --> 00:04:35,810 Now this is really cool but there are some limitations. He can't have more than say 10 customers right? 51 52 00:04:35,840 --> 00:04:41,180 Because it will be complete nightmare to try and do all of those jobs himself. 52 53 00:04:41,420 --> 00:04:43,570 And that's kind of the downside of that. 53 54 00:04:44,090 --> 00:04:52,220 If we have a large piece of code that is trying to do many many things, then it's kind of like just having 54 55 00:04:52,220 --> 00:05:00,250 one employee in your restaurant who's cooking, who's waiting on staff, who's taking payment at the cash register, 55 56 00:05:00,670 --> 00:05:06,530 who's also taking reservations and bookings. And firstly, good luck finding somebody with the CV that can 56 57 00:05:06,530 --> 00:05:08,470 do all of those things. 57 58 00:05:08,600 --> 00:05:15,380 And if you hire them, you're going to completely destroy them because one person shouldn't have to do 58 59 00:05:15,410 --> 00:05:16,700 so many things. 59 60 00:05:16,700 --> 00:05:24,050 And if they did, then you end up having a limitation in how many customers you can serve or how big your 60 61 00:05:24,050 --> 00:05:29,450 restaurant can be or how complex your app or program can be. 61 62 00:05:29,450 --> 00:05:36,250 So as your program gets more complex and more interesting, then what you want to do is you want a modular 62 63 00:05:36,250 --> 00:05:36,870 rise. 63 64 00:05:36,980 --> 00:05:43,910 You want to make it more abstract and separate out different jobs. So you'll have the waiter who actually 64 65 00:05:43,910 --> 00:05:46,400 brings the food to the customers. 65 66 00:05:46,400 --> 00:05:51,650 You'll have the pastry chef who makes the cakes. You'll have the sous-chef and the chef who does the 66 67 00:05:51,650 --> 00:05:53,070 rest of the cooking. 67 68 00:05:53,180 --> 00:05:58,670 And now, what it means is that everybody has their own jobs. 68 69 00:05:58,670 --> 00:06:04,290 They all know what they need to do and they don't get into each other's way and they don't have to all 69 70 00:06:04,310 --> 00:06:05,600 do too much at once. 70 71 00:06:06,230 --> 00:06:13,870 And now, you can expand your restaurant. You can serve more people and you can do more interesting things 71 72 00:06:14,140 --> 00:06:16,550 with more abstraction. 72 73 00:06:16,750 --> 00:06:24,940 So if you were creating a program, instead of creating a single large component which is capable of everything 73 74 00:06:24,940 --> 00:06:31,660 it's all knowing and all doing, that kind of tends to end up in what we call spaghetti code where everything 74 75 00:06:31,660 --> 00:06:33,210 is intermingled with each other. 75 76 00:06:33,220 --> 00:06:40,510 And it's a mess to try and debug when something goes wrong or when something doesn't do what you expect 76 77 00:06:40,510 --> 00:06:41,760 it to. 77 78 00:06:41,770 --> 00:06:48,940 Instead it's much better to split out functionality into different components: the chef component, the 78 79 00:06:48,940 --> 00:06:52,330 waiter component, the receptionist and the pastry chef. 79 80 00:06:52,330 --> 00:06:59,110 And this is what abstraction is all about, being able to build more complex systems by creating smaller 80 81 00:06:59,110 --> 00:07:06,640 pieces that have a defined job or defined role so that you can actually manage the complexity. 81 82 00:07:06,640 --> 00:07:10,620 What does that actually mean when we try to apply it to our code? 82 83 00:07:10,660 --> 00:07:16,560 Well at the moment, if you look inside our main.dart, you can see that it's getting pretty long right? 83 84 00:07:16,930 --> 00:07:19,380 And it's trying to do a lot of things. 84 85 00:07:19,540 --> 00:07:24,880 For example it has the questions of our question bank, 85 86 00:07:24,880 --> 00:07:32,140 it tries to track which question we're on at the moment, when we need to get a new question we have to 86 87 00:07:32,320 --> 00:07:34,330 look into our question bank. 87 88 00:07:34,600 --> 00:07:42,460 And it also does things such as putting on the layout for our app, and just you know being able to implement 88 89 00:07:42,490 --> 00:07:47,830 the buttons and all of that functionality. And it's getting a bit messy in here. 89 90 00:07:47,830 --> 00:07:54,730 If our app got any more complex, then you might start getting a bit confused about exactly what does 90 91 00:07:54,730 --> 00:07:55,250 what 91 92 00:07:55,270 --> 00:07:57,720 in this main.dart file. 92 93 00:07:58,030 --> 00:08:04,630 So let's abstract it. Let's make it more modular. In order to do that, 93 94 00:08:04,690 --> 00:08:09,900 we're going to go into our lib folder. We're going to right click and we're going to create a new Dart file. 94 95 00:08:10,480 --> 00:08:17,690 And we gonna call this Dart file quiz_brain because this is going to be the brains of our quiz. 95 96 00:08:17,710 --> 00:08:20,520 Now when I asked you if you want to add it to Git, go ahead and click 96 97 00:08:20,530 --> 00:08:21,340 Yes. 97 98 00:08:21,520 --> 00:08:25,440 And now we can create on new quiz_brain. 98 99 00:08:25,540 --> 00:08:28,810 And of course it's going to be a class. 99 100 00:08:28,960 --> 00:08:34,960 So we're going to create a class called Quiz brain. So remember classes have to start out with capitals. 100 101 00:08:35,140 --> 00:08:41,410 And in this class, we're going to define all of the things that a quiz should be and should be able to 101 102 00:08:41,410 --> 00:08:42,200 do. 102 103 00:08:42,280 --> 00:08:45,680 First of all, our quiz needs to have some questions. 103 104 00:08:45,760 --> 00:08:47,560 Let's head into our main.dart, 104 105 00:08:48,010 --> 00:08:53,560 and instead of having our question bank mixed in with all of the rest of the functionality, let's go 105 106 00:08:53,560 --> 00:09:00,150 ahead and cut it out of here and put it into where it should be inside our quiz_brain. 106 107 00:09:00,190 --> 00:09:03,600 The first thing you'll see is we have some errors here. 107 108 00:09:03,640 --> 00:09:07,120 It doesn't know what this question thing is. 108 109 00:09:07,600 --> 00:09:16,090 Well it's because we haven't actually yet imported our question.dart file which is where that class 109 110 00:09:16,360 --> 00:09:19,690 Question is defined now. 110 111 00:09:19,730 --> 00:09:26,390 While we're on the topic of our question file, the first thing to note is that at the moment, this class 111 112 00:09:26,390 --> 00:09:28,760 is doing a very very simple job. 112 113 00:09:28,760 --> 00:09:34,580 It defines the structure of every question namely that it should have a question and it should have 113 114 00:09:34,580 --> 00:09:39,750 an answer. When we're creating the constructor for our question class, 114 115 00:09:39,830 --> 00:09:42,310 we have named parameters here. 115 116 00:09:42,350 --> 00:09:49,130 So in order to create a new question object, we have to write question newQuestion let's call it equals 116 117 00:09:49,220 --> 00:09:51,180 a new question. 117 118 00:09:51,320 --> 00:09:55,490 And we have to specify that q and also the a. 118 119 00:09:56,060 --> 00:10:02,630 Now this is a bit wordy. Given that every question just has a text which is a piece of string and an 119 120 00:10:02,630 --> 00:10:04,430 answer which is a boolean, 120 121 00:10:04,430 --> 00:10:10,740 it's pretty straightforward to be able to see that if we create a question that has a string. 121 122 00:10:10,760 --> 00:10:13,790 This is the text and then it has a boolean, 122 123 00:10:13,790 --> 00:10:15,460 this is the answer. 123 124 00:10:15,500 --> 00:10:22,520 It's not completely necessary to have the parameter names q and a in there, but this is your judgment 124 125 00:10:22,550 --> 00:10:22,940 as well. 125 126 00:10:22,970 --> 00:10:25,410 So if you prefer it the other way, keep it the other way. 126 127 00:10:25,550 --> 00:10:31,610 But I find that this is much easier to read once we start building up a large bank of questions. 127 128 00:10:31,610 --> 00:10:38,260 Speaking of a large bank of questions, it's not much of a quiz app with just three questions 128 129 00:10:38,270 --> 00:10:39,230 right? 129 130 00:10:39,260 --> 00:10:46,950 So if you open up your readme file, you'll notice that we've already included some pre typed questions. 130 131 00:10:46,970 --> 00:10:48,470 There's 13 of them. 131 132 00:10:48,470 --> 00:10:54,980 And if you copy everything between the back ticks, all of those questions, you can paste it and replace 132 133 00:10:55,220 --> 00:11:01,850 what you have in your question bank at the moment. And they're already formatted so that we create a 133 134 00:11:01,850 --> 00:11:11,500 new question object with a question text and an answer. Now all we have to do is to go into our main 134 135 00:11:11,500 --> 00:11:18,670 .dart file and fix the errors that we have here. Because we deleted our question bank from our main 135 136 00:11:18,670 --> 00:11:19,900 .dart file, 136 137 00:11:19,900 --> 00:11:21,490 this no longer exists 137 138 00:11:21,490 --> 00:11:27,010 and it tells you as much. But how can we refer to our quiz_brain? 138 139 00:11:27,010 --> 00:11:35,500 Well we have to create a new quiz brain object. So first we have to go ahead and import our quiz_brain 139 140 00:11:35,500 --> 00:11:36,610 .dart, 140 141 00:11:36,610 --> 00:11:38,210 so let's put that in there. 141 142 00:11:38,350 --> 00:11:46,810 And now we're able to create a new quiz brain object which is going to be called quizBrain. 142 143 00:11:46,810 --> 00:11:52,480 And notice that when we name objects, they start out with a lowercase and we're going to set it to equal 143 144 00:11:52,570 --> 00:11:55,930 a new quiz brain object. 144 145 00:11:56,620 --> 00:12:03,880 So that constructs a new object which we can now tap into by referring to quizBrain. Down here where 145 146 00:12:03,910 --> 00:12:09,620 we have our question bank, instead of using just the question bank which should exist locally, 146 147 00:12:09,760 --> 00:12:17,140 we're going to write quizBrain.questionBank, because our question bank is now a property of our 147 148 00:12:17,140 --> 00:12:26,390 quiz brain object. And it's right here. It's a list of questions. Now we no longer need to know about questions 148 149 00:12:26,480 --> 00:12:33,320 in our main.dart file and we can delete this unused import. Notice that every time you stop requiring 149 150 00:12:33,410 --> 00:12:39,140 some code in an import, it'll tell you by making it gray and giving it a squiggly line. 150 151 00:12:39,140 --> 00:12:45,970 So let's delete that and we now need to only know about quiz brain inside our main.dart. 151 152 00:12:46,070 --> 00:12:48,460 Now we've still got a few more errors down here. 152 153 00:12:48,980 --> 00:12:55,070 So my challenge to you is to try and fix everything and run it to make sure that it still works in the 153 154 00:12:55,070 --> 00:12:55,930 same way. 154 155 00:12:56,090 --> 00:12:59,640 Pause the video and give that a go. All right. 155 156 00:12:59,670 --> 00:13:05,010 So down her, we're going to do exactly the same as what we did before. Because our question bank is now 156 157 00:13:05,040 --> 00:13:07,140 a property of our quiz brain, 157 158 00:13:07,140 --> 00:13:09,630 we have to use the dot notation instead. 158 159 00:13:09,630 --> 00:13:17,120 We have the write quizBrain.questionBank. And similarly down here, we have to write quizBrain. 159 160 00:13:17,130 --> 00:13:21,260 questionBank, so that we're actually tapping into the actual question 160 161 00:13:21,260 --> 00:13:29,680 bank. Now even though we had to add a little bit more code in our main.dart, at least our question 161 162 00:13:29,680 --> 00:13:38,230 bank which is now pretty massive, is separated into a separate class. And this now abstracts some of the 162 163 00:13:38,230 --> 00:13:44,440 functionality namely the brain for our quiz, into a separate object. 163 164 00:13:44,590 --> 00:13:51,520 And now we've abstracted our code just a little bit more. And the other benefit is that say if we were 164 165 00:13:51,520 --> 00:13:58,570 to create a different quiz, if we were to create a sports quiz for example, we don't have to change anything 165 166 00:13:58,570 --> 00:13:59,940 about our main.dart. 166 167 00:14:00,070 --> 00:14:06,640 We just have to provide a new file, say a sport quiz brain and everything in here will still work the 167 168 00:14:06,640 --> 00:14:07,420 same. 168 169 00:14:07,420 --> 00:14:11,850 And we just have to change the class that we're using to construct the quiz brain. 169 170 00:14:11,950 --> 00:14:18,910 So you could have a quiz that has lots of different types of quiz brain, sports and news and general 170 171 00:14:18,910 --> 00:14:24,730 knowledge and every time you want to switch between one to the other, you just change the object that 171 172 00:14:24,730 --> 00:14:27,420 you're actually using for the current quiz. 172 173 00:14:27,430 --> 00:14:34,510 So we've made our code base more reusable more modular and we've separated different jobs into different 173 174 00:14:34,510 --> 00:14:35,740 classes. 174 175 00:14:35,740 --> 00:14:41,590 Now if we run our app you can see that we now have more questions, 13 to be precise. 175 176 00:14:41,590 --> 00:14:44,990 But here's a problem. In our quiz app, 176 177 00:14:45,370 --> 00:14:48,430 let's say that we really hate losing. 177 178 00:14:48,430 --> 00:14:55,200 Well we can actually change the answer to our questions. 178 179 00:14:55,210 --> 00:15:01,570 So for example here we're trying to get the correct answer by tapping into our quiz brain, looking at 179 180 00:15:01,570 --> 00:15:07,330 the question bank property and picking out the current question out of the question bank, then tapping 180 181 00:15:07,330 --> 00:15:08,600 into the answer. 181 182 00:15:08,710 --> 00:15:14,620 And this is what we would call getting. We're getting the value of that correct answer. 182 183 00:15:14,890 --> 00:15:18,300 But equally I can also change it. 183 184 00:15:18,370 --> 00:15:26,290 I can simply write quiz brain question bank current answer and I can set it to a new value. I can set 184 185 00:15:26,290 --> 00:15:30,220 it to whatever it is that I picked, which in this case would be true 185 186 00:15:30,250 --> 00:15:30,550 right? 186 187 00:15:31,870 --> 00:15:42,490 Well if this happens before I check my answer, then if we hit save and we take a look at the actual correct 187 188 00:15:42,520 --> 00:15:50,560 answers and what I get as a result here, you can see that we're on this question about the loudest sound 188 189 00:15:50,620 --> 00:15:54,910 produced by an animal and the correct answer should be false. 189 190 00:15:54,910 --> 00:15:58,930 But I'm going to press true anyways and it tells me that I got it right. 190 191 00:15:59,620 --> 00:16:07,030 So how is it possible? I shouldn't be able to just change that, even if I want to be right all the time. 191 192 00:16:07,030 --> 00:16:10,130 This is not very safe for our app. 192 193 00:16:10,570 --> 00:16:17,230 Let's comment that out, and in the next lesson we're going to learn how we can make our code a bit better 193 194 00:16:17,650 --> 00:16:22,780 and conform with the second pillar of OOP, which is encapsulation. 194 195 00:16:22,780 --> 00:16:25,680 So for all of that and more, I'll see you on the next lesson. 22185

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.