All language subtitles for 11. Encapsulation 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:09,240 Now in the last lesson, I showed you that in addition to being able to get the value of any of the properties 1 2 00:00:09,630 --> 00:00:19,510 in our quiz brain, so this other class. We're currently also able to change any of the properties in our 2 3 00:00:19,530 --> 00:00:26,340 other class. And in certain cases, that can make our app behave in a way that we don't expect. 3 4 00:00:26,550 --> 00:00:32,110 And so we get onto the second pillar of OOP which is encapsulation. 4 5 00:00:32,520 --> 00:00:38,610 And just as you might want to encapsulate your workers in different office cubes so that they work on 5 6 00:00:38,610 --> 00:00:39,900 their own thing, 6 7 00:00:39,960 --> 00:00:44,310 you might also want to encapsulate your classes and your objects. 7 8 00:00:44,370 --> 00:00:49,980 So recently I came across an interesting quiz on Inc. Magazine and it was about 8 9 00:00:49,980 --> 00:00:59,820 Are you a micromanager? And it's a quiz that checks to see whether if you're basically trying to micromanage 9 10 00:00:59,910 --> 00:01:07,710 everybody else's time and you're actually trying to do everybody else's job. When we're abstracting our 10 11 00:01:07,710 --> 00:01:15,150 classes and we're trying to separate out different jobs into different roles and into different classes, 11 12 00:01:15,630 --> 00:01:23,430 what we want is for each class to have a separate role and a separate job and to keep them simple. 12 13 00:01:23,550 --> 00:01:28,370 We want them to be just concerned with their own jobs and do their own jobs well. 13 14 00:01:28,410 --> 00:01:36,420 So if we have a waiter class and we have a chef class, we don't want the waiter to be meddling with the 14 15 00:01:36,420 --> 00:01:37,230 chef's job. 15 16 00:01:37,230 --> 00:01:40,920 We don't want the waiters to just come and, 'You're not cooking it right. 16 17 00:01:41,040 --> 00:01:42,360 You should be doing it like this.' 17 18 00:01:42,360 --> 00:01:44,190 That's not the waiter's job. 18 19 00:01:44,820 --> 00:01:52,440 And this will lead to a really sad chef who will be really annoyed that he's getting micromanaged by 19 20 00:01:52,440 --> 00:01:53,320 the waiter. 20 21 00:01:53,850 --> 00:02:01,380 But it also means that the waiters job gets more complex. And if the waiter was a class, then he would 21 22 00:02:01,380 --> 00:02:06,360 have more code and it would be more intermingled with the code of the chef. 22 23 00:02:06,840 --> 00:02:14,400 And this leads to actually more complexity and not less complexity, which defeats the purpose of abstracting 23 24 00:02:14,700 --> 00:02:18,100 all classes into separate modules. 24 25 00:02:18,210 --> 00:02:19,110 What do we do? 25 26 00:02:19,110 --> 00:02:24,690 Well we could build a wall between the waiter and the chef. 26 27 00:02:24,690 --> 00:02:29,260 And this is why the kitchen is usually separate from the restaurant 27 28 00:02:29,280 --> 00:02:29,570 right? 28 29 00:02:29,760 --> 00:02:35,430 So that the chefs can do their own job and the waiters can do their own job. 29 30 00:02:35,550 --> 00:02:40,830 So the chef is only concerned with cooking food and the waiters are only concerned with getting orders 30 31 00:02:40,890 --> 00:02:43,740 and delivering the food to people. 31 32 00:02:43,740 --> 00:02:50,570 And you only have small windows between the chef and the waiter where there are things that they strictly 32 33 00:02:50,700 --> 00:02:52,700 definitely need to interact with. 33 34 00:02:52,710 --> 00:02:56,210 So when the chef passes the food to the waiter for example. 34 35 00:02:56,340 --> 00:02:58,610 So how can we do this for our code? 35 36 00:02:58,610 --> 00:03:05,880 Now we saw in the last lesson that we probably shouldn't be able to change the value of the correct 36 37 00:03:05,940 --> 00:03:08,220 answers in our main.dart right? 37 38 00:03:08,310 --> 00:03:15,510 It's none of the main.dart's business to change the question answers. It's very much to do with the 38 39 00:03:15,510 --> 00:03:20,520 quiz brain. The quiz brain should control what is true and what is false. 39 40 00:03:20,520 --> 00:03:25,810 And ideally, the answers to the questions should never really change. 40 41 00:03:25,830 --> 00:03:31,470 So what can we do in this case to encapsulate this question bank? 41 42 00:03:31,740 --> 00:03:42,210 Well, we can make this property private. And we do that in Dart by simply adding a underscore in front 42 43 00:03:42,210 --> 00:03:45,320 of the name of the property. 43 44 00:03:45,370 --> 00:03:54,010 So now, if we went back to our main.dart and we tried to meddle with the value of question bank, it 44 45 00:03:54,010 --> 00:03:56,280 doesn't work anymore. 45 46 00:03:56,330 --> 00:04:00,710 In fact, we can't even tap in to that question bank anymore. 46 47 00:04:00,710 --> 00:04:02,580 We can't say 'quiz brain 47 48 00:04:02,600 --> 00:04:09,950 give me your question bank' because question bank is now private. So it means only the quiz brain has 48 49 00:04:10,010 --> 00:04:15,740 access to the contents of this property and nobody else can. 49 50 00:04:15,740 --> 00:04:18,890 So how do we make our app still work? 50 51 00:04:18,920 --> 00:04:26,030 Well let's delegate the task of serving up these questions and answers to the person who's responsible 51 52 00:04:26,030 --> 00:04:29,330 for all things quiz related, the quiz brain. 52 53 00:04:29,390 --> 00:04:37,970 So let's create a new function. Let's create something called a getQuestionText function. And remember 53 54 00:04:37,970 --> 00:04:43,970 inside a class it's now called a method but it's basically the same thing. And our get question text 54 55 00:04:44,390 --> 00:04:49,880 needs a integer passed in, which is the current question number. 55 56 00:04:49,970 --> 00:04:55,530 So what's the question number that you actually want to get hold of? 56 57 00:04:55,580 --> 00:05:02,240 And then it's going to return or output the actual question text for that question number. 57 58 00:05:02,270 --> 00:05:09,140 So the way we would express that is we would happen to the question bank, which is private, but because 58 59 00:05:09,140 --> 00:05:16,040 we're inside the same class, we're able to access it. And then we're going to tap into the question bank 59 60 00:05:16,220 --> 00:05:19,340 at the index of question number. 60 61 00:05:19,350 --> 00:05:24,920 So which question do we want? If that was equal to zero, then we're going to get hold of this question 61 62 00:05:24,920 --> 00:05:27,950 object and then we're going to tap into it's 62 63 00:05:27,950 --> 00:05:36,560 Question text, so .questionText. And now we can go back into our main.dart, and instead of trying 63 64 00:05:36,560 --> 00:05:43,910 to get hold of this private property which is none of our business to meddle with, we can simply ask 64 65 00:05:43,910 --> 00:05:53,790 our quiz brain to get me the question text, and get me the question text at this particular question number. 65 66 00:05:54,050 --> 00:05:56,300 So to start with that will be zero. 66 67 00:05:56,630 --> 00:06:02,540 And we're going to go into our quiz brain and call get question text on quiz brain, and we're going to 67 68 00:06:02,540 --> 00:06:05,450 get the first question, and get it's 68 69 00:06:05,450 --> 00:06:13,580 questionText. So now, you can see that we've actually not only just encapsulated our question bank and 69 70 00:06:13,610 --> 00:06:20,540 it's now safe from meddling from other parties, but we've also actually abstracted our quiz app a little 70 71 00:06:20,540 --> 00:06:28,850 bit more. So now as a challenge, I want you to be able to address the other two issues that we have. 71 72 00:06:28,850 --> 00:06:30,540 Don't worry about this one. 72 73 00:06:30,890 --> 00:06:35,960 But we need to get a hold of the correct answer when the user presses true, and when the user presses 73 74 00:06:35,960 --> 00:06:42,400 false. Pause the video and complete that challenge. 74 75 00:06:42,440 --> 00:06:42,710 All right. 75 76 00:06:42,740 --> 00:06:47,720 So first things first, let's go into quiz brain and we're going to get our quiz brain to serve up the 76 77 00:06:47,720 --> 00:06:48,560 correct answer. 77 78 00:06:49,220 --> 00:06:55,580 So I'm gonna create a method that returns a boolean, and it's going to be called getCorrectAnswer or 78 79 00:06:55,580 --> 00:07:00,130 whatever it may be that you called it, doesn't really matter. And we gonna pass in 79 80 00:07:00,130 --> 00:07:06,020 again, the question number that we are trying to get the correct answer for. 80 81 00:07:06,710 --> 00:07:12,220 So now we are going to return our question bank at the 81 82 00:07:12,260 --> 00:07:14,880 question number that they provided 82 83 00:07:14,930 --> 00:07:21,820 .questionAnswer. And now we can go back and do our main.dart and we can fix our code. 83 84 00:07:21,920 --> 00:07:29,040 So instead of trying to get hold of the entire question bank into our main.dart, we're instead gonna 84 85 00:07:29,060 --> 00:07:40,130 say correct answers equal to quiz brain get me the correct answer for the current question number. And 85 86 00:07:40,130 --> 00:07:45,610 then we can compare to see if the correct answer is equal to true or down here if it's equal to False. 86 87 00:07:45,710 --> 00:07:49,680 So let's fix this in the same way down here. 87 88 00:07:49,730 --> 00:07:57,570 Correct answer should now be equal to get correct answer from the quiz brain. So now our app works 88 89 00:07:57,630 --> 00:08:03,570 and we're able to get hold of the correct answer and the correct question, but we're no longer able to 89 90 00:08:03,570 --> 00:08:06,290 just meddle with that question bank anymore. 90 91 00:08:06,330 --> 00:08:09,030 We can't say quiz brain question bank, 91 92 00:08:09,120 --> 00:08:16,080 let's just set one of your answers to true. Because our question bank is now encapsulated inside its 92 93 00:08:16,140 --> 00:08:18,870 own class and only its own class. 93 94 00:08:18,870 --> 00:08:23,220 The quiz brain can decide what it should do with this question bank. 94 95 00:08:23,520 --> 00:08:30,180 So you can't accidentally in your main.dart, for example, completely mess up and change the answers 95 96 00:08:30,180 --> 00:08:33,880 or change the questions or even delete the question bank. 96 97 00:08:33,930 --> 00:08:42,360 So we're a little bit safer because of encapsulation. If we run our app at the moment, 97 98 00:08:42,360 --> 00:08:49,620 you can see that it still works exactly the same way. But it will still crash once you get to the end 98 99 00:08:49,710 --> 00:08:51,660 of the 13 questions. 99 100 00:08:51,690 --> 00:08:58,680 We've only got 13 questions and if we try to pull out the 13th item from our question bank, that's equivalent 100 101 00:08:58,710 --> 00:09:02,820 to the 14th question because lists of course start counting from zero. 101 102 00:09:03,510 --> 00:09:06,040 So what can we do about this? 102 103 00:09:06,060 --> 00:09:14,520 Well inside our quiz brain, we should be able to check to see if our question bank still has more questions 103 104 00:09:14,520 --> 00:09:15,850 to serve up. 104 105 00:09:15,900 --> 00:09:23,400 And if so, then we'll serve it up but otherwise we should protect the app by not trying to get hold of 105 106 00:09:23,400 --> 00:09:26,310 a question that doesn't actually exist. 106 107 00:09:26,330 --> 00:09:28,350 So how can we do this? 107 108 00:09:28,350 --> 00:09:34,980 Well instead of keeping track of which question we're on in our main.dart which is not really its 108 109 00:09:34,980 --> 00:09:43,310 job, let's remove this and let's put it into our quiz brain. So I'm going to put it inside my quiz brain 109 110 00:09:43,310 --> 00:09:49,880 class as a property and in fact I'm going to make it a private property. 110 111 00:09:49,880 --> 00:09:56,780 So only the quiz brain has access to this. So I can't accidentally say in main.dart or in question 111 112 00:09:56,780 --> 00:10:04,250 .dart, suddenly sets my question number to 12 or 13 which will crash my app. And instead of 112 113 00:10:04,520 --> 00:10:11,900 calling getQuestionText and providing the question number from our main.dart, I'm going to use our 113 114 00:10:11,900 --> 00:10:21,650 questionNumber inside our quiz brain. So we need to change it here to that private question number. And 114 115 00:10:22,190 --> 00:10:30,290 in order to go to the next question, I'm going to create a method that's just called nextQuestion and 115 116 00:10:30,560 --> 00:10:38,210 all it does is it checks to see if it's safe to actually go to the next question. And that means we have 116 117 00:10:38,210 --> 00:10:48,010 to check to see if the questionNumber is less than our questionBank.length. 117 118 00:10:48,650 --> 00:10:52,660 So we know that .length gives us the size of the questionBank. 118 119 00:10:52,700 --> 00:10:58,220 So this is going to be 13 because we have 13 items in our question bank. 119 120 00:10:58,580 --> 00:11:06,020 And when our question number is less than that, then theoretically, it means that it should be safe to 120 121 00:11:06,020 --> 00:11:07,370 go to the next question. 121 122 00:11:07,790 --> 00:11:09,670 So then we could say 122 123 00:11:09,770 --> 00:11:17,450 questionNumber++. And this should increment our questionNumber every time we need to go to 123 124 00:11:17,450 --> 00:11:18,820 the next question. 124 125 00:11:18,920 --> 00:11:28,070 So in our main.dart instead of updating our questionNumber, we're going to instead call upon our 125 126 00:11:28,070 --> 00:11:37,640 quiz brain to go to the next question instead. And we don't need to deal with our question numbers or 126 127 00:11:37,640 --> 00:11:41,240 keeping track of it inside our main.dart anymore. 127 128 00:11:42,200 --> 00:11:43,510 Let's go ahead and change that 128 129 00:11:43,510 --> 00:11:44,940 in both places. 129 130 00:11:45,020 --> 00:11:53,210 So it's gonna be quizBrain.nextQuestion. And we're gonna delete this print statement and also remember 130 131 00:11:53,210 --> 00:12:01,460 that get question answer no longer need an input and also get question text no longer needs an input. 131 132 00:12:01,460 --> 00:12:11,130 So now let's run up from the start by clicking either the run there or clicking our hot restart. And we 132 133 00:12:11,130 --> 00:12:13,800 should start off from the beginning of our quiz. 133 134 00:12:13,800 --> 00:12:21,600 So now if we go through our quiz and we get to the end you can see that our app still crashes. It's still 134 135 00:12:21,600 --> 00:12:28,730 saying that we're trying to get the 13th element of our list which only goes up to 12. 135 136 00:12:29,490 --> 00:12:31,810 So what's the reason here? 136 137 00:12:32,130 --> 00:12:39,570 Well let's add a print statement right here, so that we get to see what question number is. 137 138 00:12:39,990 --> 00:12:51,730 And let's also print our questionBank.length. And now let's run our app again from the start, so 138 139 00:12:51,730 --> 00:12:57,960 that the first time when we click true and we go to the next question, the question number goes to one. 139 140 00:12:57,970 --> 00:13:05,500 So we go to the next question and we have a total of 13 questions. So let's keep hitting true until we 140 141 00:13:05,500 --> 00:13:07,860 get close to crashing the app. 141 142 00:13:09,210 --> 00:13:17,340 So now currently, our question number is 12. And it means that we're trying to pull out the 12th item 142 143 00:13:17,430 --> 00:13:19,740 out of our questionBank which still exists. 143 144 00:13:19,740 --> 00:13:21,600 It's this last question right here, 144 145 00:13:21,690 --> 00:13:23,070 the question about West Virginia. 145 146 00:13:26,000 --> 00:13:33,080 The next time that I click the true button, it will trigger the next question method. And now it's going 146 147 00:13:33,070 --> 00:13:38,800 to check to see if the question number at this point, which is 12, is less than 147 148 00:13:38,810 --> 00:13:41,440 questionBank.length which is 13. 148 149 00:13:41,480 --> 00:13:42,720 This is true. 149 150 00:13:42,800 --> 00:13:48,420 So it's going to go to the next question and then it's going to update the question text. And it's going 150 151 00:13:48,420 --> 00:13:54,050 to try and pull out the 13th item out of our list, which doesn't exist. 151 152 00:13:54,050 --> 00:13:58,610 So right now, this is the point when our app crashes. 152 153 00:13:58,670 --> 00:14:00,690 So how can we solve this problem? 153 154 00:14:01,180 --> 00:14:09,730 Well, we can simply make our questionBank.length check it for one less than the size of the questionBank. 154 155 00:14:10,040 --> 00:14:15,210 Now we never actually get to 13. 155 156 00:14:15,380 --> 00:14:19,980 We get to 12 and the next time I click it, nothing else happens. 156 157 00:14:20,090 --> 00:14:24,620 We are at the end of the quiz and our app no longer crashes. 157 158 00:14:24,620 --> 00:14:34,240 Perfect. So now our app is being safety-checked inside our quiz brain, which is the logical place that 158 159 00:14:34,240 --> 00:14:36,390 should deal with all things quiz related. 159 160 00:14:36,880 --> 00:14:43,990 And our main.dart simply just is concerned with displaying things on screen or clicking a button 160 161 00:14:43,990 --> 00:14:49,450 to go to the next question, and it doesn't have to deal with the quiz functionality anymore. 18459

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