All language subtitles for 11. Encapsulation in Action

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
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
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French Download
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranî)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

0 Now in the last lesson, I showed you that in addition to being able to get the value of any of the properties 1

in our quiz brain, so this other class. We're currently also able to change any of the properties in our 2

other class. And in certain cases, that can make our app behave in a way that we don't expect. 3

And so we get onto the second pillar of OOP which is encapsulation. 4

And just as you might want to encapsulate your workers in different office cubes so that they work on 5

their own thing, 6

you might also want to encapsulate your classes and your objects. 7

So recently I came across an interesting quiz on Inc. Magazine and it was about 8

Are you a micromanager? And it's a quiz that checks to see whether if you're basically trying to micromanage 9

everybody else's time and you're actually trying to do everybody else's job. When we're abstracting our 10

classes and we're trying to separate out different jobs into different roles and into different classes, 11

what we want is for each class to have a separate role and a separate job and to keep them simple. 12

We want them to be just concerned with their own jobs and do their own jobs well. 13

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

chef's job. 15

We don't want the waiters to just come and, 'You're not cooking it right. 16

You should be doing it like this.' 17

That's not the waiter's job. 18

And this will lead to a really sad chef who will be really annoyed that he's getting micromanaged by 19

the waiter. 20

But it also means that the waiters job gets more complex. And if the waiter was a class, then he would 21

have more code and it would be more intermingled with the code of the chef. 22

And this leads to actually more complexity and not less complexity, which defeats the purpose of abstracting 23

all classes into separate modules. 24

What do we do? 25

Well we could build a wall between the waiter and the chef. 26

And this is why the kitchen is usually separate from the restaurant 27

right? 28

So that the chefs can do their own job and the waiters can do their own job. 29

So the chef is only concerned with cooking food and the waiters are only concerned with getting orders 30

and delivering the food to people. 31

And you only have small windows between the chef and the waiter where there are things that they strictly 32

definitely need to interact with. 33

So when the chef passes the food to the waiter for example. 34

So how can we do this for our code? 35

Now we saw in the last lesson that we probably shouldn't be able to change the value of the correct 36

answers in our main.dart right? 37

It's none of the main.dart's business to change the question answers. It's very much to do with the 38

quiz brain. The quiz brain should control what is true and what is false. 39

And ideally, the answers to the questions should never really change. 40

So what can we do in this case to encapsulate this question bank? 41

Well, we can make this property private. And we do that in Dart by simply adding a underscore in front 42

of the name of the property. 43

So now, if we went back to our main.dart and we tried to meddle with the value of question bank, it 44

doesn't work anymore. 45

In fact, we can't even tap in to that question bank anymore. 46

We can't say 'quiz brain 47

give me your question bank' because question bank is now private. So it means only the quiz brain has 48

access to the contents of this property and nobody else can. 49

So how do we make our app still work? 50

Well let's delegate the task of serving up these questions and answers to the person who's responsible 51

for all things quiz related, the quiz brain. 52

So let's create a new function. Let's create something called a getQuestionText function. And remember 53

inside a class it's now called a method but it's basically the same thing. And our get question text 54

needs a integer passed in, which is the current question number. 55

So what's the question number that you actually want to get hold of? 56

And then it's going to return or output the actual question text for that question number. 57

So the way we would express that is we would happen to the question bank, which is private, but because 58

we're inside the same class, we're able to access it. And then we're going to tap into the question bank 59

at the index of question number. 60

So which question do we want? If that was equal to zero, then we're going to get hold of this question 61

object and then we're going to tap into it's 62

Question text, so .questionText. And now we can go back into our main.dart, and instead of trying 63

to get hold of this private property which is none of our business to meddle with, we can simply ask 64

our quiz brain to get me the question text, and get me the question text at this particular question number. 65

So to start with that will be zero. 66

And we're going to go into our quiz brain and call get question text on quiz brain, and we're going to 67

get the first question, and get it's 68

questionText. So now, you can see that we've actually not only just encapsulated our question bank and 69

it's now safe from meddling from other parties, but we've also actually abstracted our quiz app a little 70

bit more. So now as a challenge, I want you to be able to address the other two issues that we have. 71

Don't worry about this one. 72

But we need to get a hold of the correct answer when the user presses true, and when the user presses 73

false. Pause the video and complete that challenge. 74

All right. 75

So first things first, let's go into quiz brain and we're going to get our quiz brain to serve up the 76

correct answer. 77

So I'm gonna create a method that returns a boolean, and it's going to be called getCorrectAnswer or 78

whatever it may be that you called it, doesn't really matter. And we gonna pass in 79

again, the question number that we are trying to get the correct answer for. 80

So now we are going to return our question bank at the 81

question number that they provided 82

.questionAnswer. And now we can go back and do our main.dart and we can fix our code. 83

So instead of trying to get hold of the entire question bank into our main.dart, we're instead gonna 84

say correct answers equal to quiz brain get me the correct answer for the current question number. And 85

then we can compare to see if the correct answer is equal to true or down here if it's equal to False. 86

So let's fix this in the same way down here. 87

Correct answer should now be equal to get correct answer from the quiz brain. So now our app works 88

and we're able to get hold of the correct answer and the correct question, but we're no longer able to 89

just meddle with that question bank anymore. 90

We can't say quiz brain question bank, 91

let's just set one of your answers to true. Because our question bank is now encapsulated inside its 92

own class and only its own class. 93

The quiz brain can decide what it should do with this question bank. 94

So you can't accidentally in your main.dart, for example, completely mess up and change the answers 95

or change the questions or even delete the question bank. 96

So we're a little bit safer because of encapsulation. If we run our app at the moment, 97

you can see that it still works exactly the same way. But it will still crash once you get to the end 98

of the 13 questions. 99

We've only got 13 questions and if we try to pull out the 13th item from our question bank, that's equivalent 100

to the 14th question because lists of course start counting from zero. 101

So what can we do about this? 102

Well inside our quiz brain, we should be able to check to see if our question bank still has more questions 103

to serve up. 104

And if so, then we'll serve it up but otherwise we should protect the app by not trying to get hold of 105

a question that doesn't actually exist. 106

So how can we do this? 107

Well instead of keeping track of which question we're on in our main.dart which is not really its 108

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

class as a property and in fact I'm going to make it a private property. 110

So only the quiz brain has access to this. So I can't accidentally say in main.dart or in question 111

.dart, suddenly sets my question number to 12 or 13 which will crash my app. And instead of 112

calling getQuestionText and providing the question number from our main.dart, I'm going to use our 113

questionNumber inside our quiz brain. So we need to change it here to that private question number. And 114

in order to go to the next question, I'm going to create a method that's just called nextQuestion and 115

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

to check to see if the questionNumber is less than our questionBank.length. 117

So we know that .length gives us the size of the questionBank. 118

So this is going to be 13 because we have 13 items in our question bank. 119

And when our question number is less than that, then theoretically, it means that it should be safe to 120

go to the next question. 121

So then we could say 122

questionNumber++. And this should increment our questionNumber every time we need to go to 123

the next question. 124

So in our main.dart instead of updating our questionNumber, we're going to instead call upon our 125

quiz brain to go to the next question instead. And we don't need to deal with our question numbers or 126

keeping track of it inside our main.dart anymore. 127

Let's go ahead and change that 128

in both places. 129

So it's gonna be quizBrain.nextQuestion. And we're gonna delete this print statement and also remember 130

that get question answer no longer need an input and also get question text no longer needs an input. 131

So now let's run up from the start by clicking either the run there or clicking our hot restart. And we 132

should start off from the beginning of our quiz. 133

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

saying that we're trying to get the 13th element of our list which only goes up to 12. 135

So what's the reason here? 136

Well let's add a print statement right here, so that we get to see what question number is. 137

And let's also print our questionBank.length. And now let's run our app again from the start, so 138

that the first time when we click true and we go to the next question, the question number goes to one. 139

So we go to the next question and we have a total of 13 questions. So let's keep hitting true until we 140

get close to crashing the app. 141

So now currently, our question number is 12. And it means that we're trying to pull out the 12th item 142

out of our questionBank which still exists. 143

It's this last question right here, 144

the question about West Virginia. 145

The next time that I click the true button, it will trigger the next question method. And now it's going 146

to check to see if the question number at this point, which is 12, is less than 147

questionBank.length which is 13. 148

This is true. 149

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

to try and pull out the 13th item out of our list, which doesn't exist. 151

So right now, this is the point when our app crashes. 152

So how can we solve this problem? 153

Well, we can simply make our questionBank.length check it for one less than the size of the questionBank. 154

Now we never actually get to 13. 155

We get to 12 and the next time I click it, nothing else happens. 156

We are at the end of the quiz and our app no longer crashes. 157

Perfect. So now our app is being safety-checked inside our quiz brain, which is the logical place that 158

should deal with all things quiz related. 159

And our main.dart simply just is concerned with displaying things on screen or clicking a button 160

to go to the next question, and it doesn't have to deal with the quiz functionality anymore.

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