All language subtitles for 9. [Dart] Classes and Objects

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
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 All right. 1

So now that we've seen classes and objects in action and we've used them a little bit, it's time to do 2

a deep dive and understand exactly how classes and objects work in Dart. 3

So firstly a class is simply just a blueprint and it's used to create all of the things that we're going 4

to be showing in our app and the user is going to be interacting with. So essentially, everything that 5

makes up our app is going to be created using a blueprint that's called a class. 6

Now what exactly is an object then? 7

Well when we take that blueprint and we take it to the factory and the actual car, in this instance, gets 8

built, well that's the object. That's the actual thing that's going to be doing the driving, or in our 9

case of an app, it'll be the actual button that can receive taps and the images that actually change. 10

Those are the actual objects. But because we can't see the objects until the app is running, we have to 11

plan out how they will function and how they will work by creating these classes. 12

A class has two really important things. One are its properties. 13

So if we're building out a car, then that will be its color or the number of seats it has or the number 14

wheels it has or the number of doors it has or all sorts of things that are things to do with the 15

characteristics of the object. And in Dart, these things that we've been referring to as variables when 16

they're inside a class, 17

they're now called properties or fields. 18

Now the other important part of a class that we need to define are its methods. 19

So the things that it can do. The actions that the object that will eventually get created, will be able 20

to perform. 21

In the case of our car object, that might be to drive, or to brake, or to turn around. 22

And these methods are actions that the object will be able to do that we're going to define in the blueprint 23

even before the object gets built. 24

So for example, you might see a class defined like this. 25

Firstly you have that class keyword which shows that everything else that's coming afterwards is going 26

to be a blueprint. 27

And then you have the name of the class. 28

So in this case we're building a car blueprint. And by convention in Dart as with many other languages, 29

when you're creating a variable say here we've got numberOfDoors, or when you're creating a function, 30

say here we've got drive, 31

those are all camel case 32

starting with a lowercase word. 33

But when you're creating a class to show that it's different, we always start-off with a capital letter. 34

So all the words in your class names are capitalized. 35

Now here's our Car class 36

and inside these curly braces are the definitions for this car class. 37

So this is the blueprint of exactly how our car will look and also how it will behave. 38

And between the looks and the behavior, we end up with a class. 39

So in this case we have just a single property, which is called numberOfDoors. 40

And this defines a characteristic of the car namely, how many doors it has. 41

This car has five. 42

This car also has a function called drive, 43

and this defines a little bit of functionality for the car. 44

So namely this car, when we call this thing called drive, it's going to start turning the wheels for example. 45

Now notice how inside a class, our variables are no longer called variables and they're called properties 46

or fields. 47

Similarly, a function when it's defined inside the curly braces of a class, it's now called a method and 48

not a function anymore. 49

So if you're new to programming there's a lot of new words and it's a good idea to build your own glossary 50

of words because it's just like learning a new language. 51

When people refer to a method, you can think of them as just a function that's associated with the class. 52

And when people are talking about a property, it's simply a variable that's associated with the class. 53

And once you hear it often enough, then it'll start becoming second nature. 54

Now once we have our object, it's the object that actually performed those pre-planned actions in the 55

class such as moving, right? 56

Or it actually has those characteristics which we defined in our class such as its color property or 57

the numberOfDoors property. The two paths of creating a object are first, we have to create the class 58

and this is gonna be the blueprint that's going to define everything about the eventual object that 59

will get created. 60

So we use the class keyword, we give the class a capitalized name and we define variables in it which 61

are properties for that class. 62

And we also give it functions which become the methods that the class can do. 63

And when we actually create the object from this class, then our code looks something like this. 64

We would define the type, which is in this case, the name of the class and then we would give our object 65

a name. 66

And finally we would set it to equal a new version of that class. When we have the word car and these 67

empty parentheses, 68

it's basically creating a new object from this blueprint. 69

It's like sending that blueprint to a factory and out comes our actual car object. 70

Now this is all very abstract and we're having to imagine a lot of it in our head. 71

So let's put it down onto code and head over to our trusty DartPad to be up to see this in action and 72

understand a little bit better. 73

So I've got my empty main method up here, and I'm going to create a new class right below it. 74

I'm going to call that class Human. 75

So remember it should be capitalized. 76

And this class of humans are going to have some properties, so some characteristics. 77

So what kind of characteristics might a human have? 78

Well they might have a height right? 79

We could create a height property and maybe it'll have an age property. And when we create a new class, 80

we can either create these properties as empty or null such as here, or we can say that by default when 81

we create a new human, it should always have a height. 82

Let's say that when a default human gets created, it starts out being, I don't know ,maybe 15 centimetres 83

in height, and maybe it will have a default age of zero. 84

So now if I go into my main method and I create a new object from this human, let's say that we create 85

a new human, 86

so that's gonna be the type of this object. And our actual human, let's call her 87

Jenny, is going to be created using our human class. And we do that of course by writing human and then 88

we have a set of empty parentheses. 89

And this builds out our new human. 90

So human is the blueprint for all humans. 91

They all should have a height and they all should have an age. 92

And we have some default values for how humans should be when they first get built. 93

And Jenny is our actual object who's built from this blueprint. And we're doing things in our program, 94

we're actually interacting with Jenny rather than the sort of abstract blueprint kind of for human. So 95

we might say some things such as, let's print Jenny's height. So we have our print statement and then 96

we're going to tap into our Jenny object and then we're gonna use the dot notation to tap into its property 97

namely the height property. 98

So we'll say jenny.height and let's print that into the console. And you can see that because we've 99

given all humans a default starting height of 15, 100

Jenny also has a height of 15. 101

Now a little bit later on, I could maybe change that. 102

Maybe Jenny grows a bit and lets say that her height is now 20cm. 103

So I can change all of these properties and I can tap into them simply by using that dot notation. By 104

saying that this is the object that I'm concerned about and it finds the object which we know gets created 105

from the human class, and by default, all of the properties of a human 106

start out with these values. Then I'm saying get me Jenny's height. And that's what that dot says. 107

It's just saying, 'this is the property associated with Jenny that I want to know about.' 108

Or 109

later on when our human get some functions associated with it, you'll see that the way that we tap into 110

it is still with the dot notation. 111

So when you see something.something, it usually means that this is an object and we're tapping into 112

one of its properties or methods. 113

Now at the moment, every single new human that we create starts out with a height of 15cm. 114

Now that might not be true 115

right? 116

So let's say that we created another human and this is James. 117

And he's also created from the human class, which is what this part means. We're going to the human factory 118

and we're building a new human. 119

And if I was to print his height, you'll see that it's exactly the same. 120

He's also 15 centimetres tall. But that's probably not really true 121

right? 122

Why should it be that when a new human gets created, they all have the same default height? 123

What if we wanted to initialize each object with a different value for some of these properties? 124

How would we do that? 125

Well we already saw that in the last lesson that we can use something called a constructor for our class. 126

And to create a constructor, 127

we would write the name of the class, so it's human, and then we would create a set of empty parentheses. 128

And just as when we create a function we provide some inputs into these parentheses, when we create a 129

constructor for a class we can provide some inputs to our class. 130

So we could say that maybe the starting height property which is going to be a double, because our height 131

property is a double. 132

We'll give it a name of startingHeight. 133

And now inside the curly braces, we can assign these startingHeight to the height, and height can start 134

out without a value. It'll start out being null. 135

But when we create our new human, we have to provide a starting height and then we can say that the humans 136

height property will be set to whatever the starting height is that we provided. 137

So now we get some errors here because it needs a starting height and we haven't given it one. 138

So let's add that in. So we could say that Jenny is a human who starts out being 15 centimeters but James 139

starts out being a human who is, maybe he's a bit taller. Maybe he's 20 centimeters. 140

So now when we click run, you can see that Jenny's height starts out 15cm and James's height starts out 141

at 20cm. 142

And our code now makes a little bit more sense because why should a human have a default height 143

right? 144

They they don't all get created with the same height. And just as we could make the input or the parameter 145

a named parameter, in our functions we can do the same in our class constructor. So we can add a set of 146

curly braces around these inputs to say that when we use it when we create our new human object, we have 147

to specify that name. 148

So startingHeight and then a colon. 149

So this way it's a bit easier on the eye and you can see that I'm creating a new human and I'm providing 150

a starting height for that human. As I said earlier, our classes can have properties and it can also have 151

methods. Our methods are simply functions that are defined inside a class. 152

If we were to create a method let's say, talk to get a little human to say something, we can tell 153

it what to say. 154

So we'll create a string input called whatToSay, and then inside the talk function or in this case the 155

talk method because we're talking about a function associated with the class, we can say that we'll just 156

print the string that it's told to say. 157

So that means we can tell our new humans, say for example, let's get rid of James. Let's just say Jenny 158

.talk and the thing that you should say is ,Why is this sky blue?' 159

Now when we run our code, we print Jenny's height and we get Jenny to talk. And you can see that by calling 160

this method talk, then this object actually performs that action and it prints out the thing that we 161

want it to say into the console. 162

This is now a method that's associated with the human class. 163

And just as we use the dot notation to access the property height, we're also using the dot notation 164

to trigger the talk method. 165

So when we're working with objects you see the dot all over the place and it just refers to saying, 'this 166

object do this or this object get me it's property.' 167

So we now have a pretty simple class. And this class is our blueprint for how a human object should behave 168

and what characteristics it should have. 169

So all of the things that a human object, once it gets created, should be able to do and what it should 170

be. You can of course make this as complex as you need it to be. But in essence, we're simply creating 171

a plan for how this object should behave or how it should appear. And we use it by creating it as a new 172

object and we can create as many of these as we need to and get them to interact with each other or 173

get them to do specific tasks. 174

And so this is how we would create our classes and objects. 175

But why exactly do we need these classes and objects? To find out about that, 176

I'll see you on the next lesson.

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