1
00:00:08,630 --> 00:00:11,330
Here we are, in the second week of the
class.

2
00:00:11,330 --> 00:00:16,590
And starting on module four, which is
about classes and objects.

3
00:00:16,590 --> 00:00:20,140
Just to let you know the learning
objectives for module four.

4
00:00:20,140 --> 00:00:20,760
Here they are.

5
00:00:21,860 --> 00:00:26,570
This is the only module that has an
objective about creating.

6
00:00:26,570 --> 00:00:30,809
We're going to be designing a class
together at the end of this module.

7
00:00:30,809 --> 00:00:33,824
But we'll also be applying the knowledge
you learned

8
00:00:33,824 --> 00:00:36,840
by using existing classes that I'll
provide to you.

9
00:00:40,530 --> 00:00:43,158
In this module, we're going to talk about

10
00:00:43,158 --> 00:00:47,820
the foundational ideas behind the object
oriented paradigm.

11
00:00:47,820 --> 00:00:49,490
So here's the big idea.

12
00:00:49,490 --> 00:00:52,930
The big idea is that software can be
implemented

13
00:00:52,930 --> 00:00:58,050
as a collection of objects that interact
with each other.

14
00:00:58,050 --> 00:01:01,725
So, this paradigm is very well suited to
game development

15
00:01:01,725 --> 00:01:05,325
because if you think about it, games are
virtual world,

16
00:01:05,325 --> 00:01:10,201
where there's a bunch of different
entities that interact with each other.

17
00:01:10,201 --> 00:01:13,533
If you're walking along in the virtual
world and you see an

18
00:01:13,533 --> 00:01:17,514
Orc, you might decide to interact with
that Orc in a particular way.

19
00:01:17,514 --> 00:01:21,348
With your bare hands, or your sword of
massive cutting or whatever.

20
00:01:21,348 --> 00:01:25,716
If you see a teddy bear in the game world,
you may decide to interact

21
00:01:25,716 --> 00:01:30,552
to that teddy bear in a particular way, so
you might hug the teddy bear, or

22
00:01:30,552 --> 00:01:34,608
you might explode the teddy bear, or you
might light the teddy

23
00:01:34,608 --> 00:01:39,180
bear on fire, or you'd interact with that
object in some way.

24
00:01:39,180 --> 00:01:43,960
And even non-player characters, right, the
NPCs interact with each other.

25
00:01:43,960 --> 00:01:46,640
In with you as part of the game world.

26
00:01:46,640 --> 00:01:51,280
So there's lots of interactions between
objects or entities in game

27
00:01:51,280 --> 00:01:55,920
world, so modeling those game worlds in
our software as interacting

28
00:01:55,920 --> 00:01:59,760
objects is a really powerful technique
that we can use.

29
00:02:01,920 --> 00:02:05,351
We'll start off with an in-lecture quiz so
you can tell

30
00:02:05,351 --> 00:02:10,010
me a little bit about what you already
understand about software objects.

31
00:02:16,630 --> 00:02:17,770
Okay.

32
00:02:17,770 --> 00:02:21,834
So, software objects have three different
things.

33
00:02:21,834 --> 00:02:27,660
They have state, they have behavior, and
they have identity.

34
00:02:27,660 --> 00:02:31,040
And we can relate this to objects in the
real world as well.

35
00:02:31,040 --> 00:02:35,970
So the state of an object is the
characteristics of that object.

36
00:02:35,970 --> 00:02:37,380
Think of yourself right now.

37
00:02:37,380 --> 00:02:39,690
You have a number of different
characteristics.

38
00:02:39,690 --> 00:02:42,120
You have a height, you have a weight.

39
00:02:42,120 --> 00:02:46,850
You have a degree of hungriness, and
perhaps a degree of boredom.

40
00:02:46,850 --> 00:02:48,170
How cruel.

41
00:02:48,170 --> 00:02:51,329
But these are different characteristics
that

42
00:02:51,329 --> 00:02:54,430
you have that potentially change over
time.

43
00:02:55,720 --> 00:02:58,779
You might grow taller or you might shrink.
You might gain or lose weight and so on.

44
00:03:00,220 --> 00:03:06,080
So the state of an object is really all
about the characteristics of that object.

45
00:03:07,360 --> 00:03:09,770
Objects also have behavior.

46
00:03:09,770 --> 00:03:13,570
So there are things that we can do to
objects,

47
00:03:13,570 --> 00:03:17,329
like we could take a teddy bear and throw
it.

48
00:03:18,510 --> 00:03:20,880
And there are things that we can tell

49
00:03:20,880 --> 00:03:23,724
objects to do to themselves, so when we
get

50
00:03:23,724 --> 00:03:26,805
to XNA in particular we'll tell an object
to,

51
00:03:26,805 --> 00:03:30,330
perhaps we tell a teddy bear to draw
itself.

52
00:03:30,330 --> 00:03:32,631
And so we don't have to worry about the
details

53
00:03:32,631 --> 00:03:36,140
of how a teddy bear draws itself.
We just tell it to do so.

54
00:03:36,140 --> 00:03:39,195
Similarly, if we had a deck of cards, we
might just tell

55
00:03:39,195 --> 00:03:43,260
that deck, go shuffle yourself and we
don't care how it happens.

56
00:03:43,260 --> 00:03:46,130
It's just that it happens internally to
the object.

57
00:03:46,130 --> 00:03:48,370
So, objects have behavior as well.

58
00:03:48,370 --> 00:03:52,550
They have things they can do or that we
can do to them.

59
00:03:52,550 --> 00:03:57,734
And finally, objects have identity.
We will end up or we'll discover that

60
00:03:57,734 --> 00:04:04,130
we create objects, as we go along in our
software as our software program runs.

61
00:04:04,130 --> 00:04:09,270
And each of those objects lives, if you
will in a different place in memory.

62
00:04:09,270 --> 00:04:14,690
And so each of them are distinct from each
other and so they have identity.

63
00:04:14,690 --> 00:04:16,380
And that's good too, right.

64
00:04:16,380 --> 00:04:19,530
If you are in a traditional classroom and
somebody

65
00:04:19,530 --> 00:04:22,755
wanted to call on a particular student,
you can't

66
00:04:22,755 --> 00:04:28,030
just yell out the question, you have to
first say which student you mean.

67
00:04:28,030 --> 00:04:30,080
And then ask your question.

68
00:04:30,080 --> 00:04:33,284
Similarly with objects, if we want them to

69
00:04:33,284 --> 00:04:36,755
do something, we need to specify which
object

70
00:04:36,755 --> 00:04:42,860
we expect to do that thing before we, sort
of access the behavior of that object.

71
00:04:42,860 --> 00:04:46,776
So identity is really important as well.
So all objects have

72
00:04:46,776 --> 00:04:52,437
state, behavior and identity.
That's a language-independent,

73
00:04:52,437 --> 00:04:58,500
object oriented set of concepts that
really has nothing to do with C#.

74
00:04:58,500 --> 00:05:04,160
The next slide uses some C# specific
terminology.

75
00:05:04,160 --> 00:05:08,610
So let's actually have a concrete example.
We're going to have a playing card object.

76
00:05:08,610 --> 00:05:13,220
And that playing card object is going to
have state, behavior and identity.

77
00:05:13,220 --> 00:05:18,070
And the state of that playing card will be
stored in fields.

78
00:05:18,070 --> 00:05:21,931
That's a C# piece of terminology, but I'll

79
00:05:21,931 --> 00:05:26,360
tell you right now fields are just
variables.

80
00:05:26,360 --> 00:05:31,936
We learned in the last module how we can
declare and use variables while the fields

81
00:05:31,936 --> 00:05:38,310
in an object are just variables.
Externally from the object the

82
00:05:38,310 --> 00:05:43,370
way we'll let sort of consumers of this
object.

83
00:05:43,370 --> 00:05:46,404
So the game for example has a playing card
in it and

84
00:05:46,404 --> 00:05:50,288
the game needs to know something about the
state of this card.

85
00:05:50,288 --> 00:05:58,900
We almost never will let consumers of this
object access those fields directly.

86
00:05:58,900 --> 00:06:03,435
That's really bad programming practice.
So instead what we'll do is we

87
00:06:03,435 --> 00:06:08,308
will expose, that's the terminology we
use, we will expose that state.

88
00:06:08,308 --> 00:06:13,028
Through C# properties if you've programmed
in other languages

89
00:06:13,028 --> 00:06:17,428
like Java for example, you've written
getters and setters, or

90
00:06:17,428 --> 00:06:20,868
accessors and mutators all the time, in C#
we have

91
00:06:20,868 --> 00:06:25,555
this concept called properties, that does
exactly the same thing.

92
00:06:25,555 --> 00:06:29,200
It let's us access, read, write or both
particular

93
00:06:29,200 --> 00:06:35,020
pieces of state for the particular object.
So, that state.

94
00:06:35,020 --> 00:06:38,520
And for a card the particular things we
might store for state

95
00:06:38,520 --> 00:06:42,280
are the rank of the card, Ace, King, Queen
and so on.

96
00:06:42,280 --> 00:06:45,672
The suit of the card, depending on the
game you're playing.

97
00:06:45,672 --> 00:06:48,120
you know, grass, water, fire or perhaps

98
00:06:48,120 --> 00:06:51,112
hearts, clubs, spades and diamonds and
whether or

99
00:06:51,112 --> 00:06:54,580
not the card is face up or not which would
matter to us if we're

100
00:06:54,580 --> 00:06:56,235
actually drawing the card.

101
00:06:56,235 --> 00:07:00,390
So, you know you draw a face down card
nobody can tell what it is.

102
00:07:00,390 --> 00:07:03,640
You draw a face up card and everyone can
tell what it is.

103
00:07:03,640 --> 00:07:04,140
Okay.

104
00:07:04,140 --> 00:07:08,654
So that's state, there's also behavior
associated with the object,

105
00:07:08,654 --> 00:07:13,120
and in C#, we expose behavior through
things called methods.

106
00:07:13,120 --> 00:07:16,968
Other languages used terminology like
method or functions,

107
00:07:16,968 --> 00:07:20,002
their methods in C#, and the method that
we'll

108
00:07:20,002 --> 00:07:22,889
have for our cargo, will be the flip it
over.

109
00:07:23,920 --> 00:07:27,139
And finally cards get their identity when
we

110
00:07:27,139 --> 00:07:31,650
create new card objects and that's called
instantiation.

111
00:07:35,410 --> 00:07:38,050
So, another opportunity to do an in

112
00:07:38,050 --> 00:07:42,660
lecture quiz addressing particularly the
state of objects.

113
00:07:47,550 --> 00:07:52,830
So conceptually for our card what we have
is, inside

114
00:07:52,830 --> 00:07:58,110
here, hidden inside the card, we have
those fields

115
00:07:58,110 --> 00:08:03,400
that we don't let anyone access
externally.

116
00:08:03,400 --> 00:08:08,496
And by the way, having the fields and
around the edge here, around

117
00:08:08,496 --> 00:08:13,680
the edge here, all of these different
properties and behaviors.

118
00:08:14,950 --> 00:08:18,826
Bundling all that stuff together is called
encapsulation,

119
00:08:18,826 --> 00:08:21,714
because we've put the state and the
behavior

120
00:08:21,714 --> 00:08:24,298
all into a single capsule, if you will,

121
00:08:24,298 --> 00:08:28,400
that object that carries all the stuff
around.

122
00:08:28,400 --> 00:08:34,897
The other thing that's important, though,
is as we access things like properties,

123
00:08:34,897 --> 00:08:40,326
like the Suit property here, or as we call
a method, the FlipOver method

124
00:08:40,326 --> 00:08:46,378
here, the other thing that we know is that
the consumer of this object, doesn't

125
00:08:46,378 --> 00:08:52,640
know anything about the internal details.
They have no idea how it's implemented.

126
00:08:52,640 --> 00:08:58,080
They have no idea what data types the
Rank, Suit, and FaceUp are.

127
00:08:58,080 --> 00:09:01,346
There are logical choices, but we don't
know, and so

128
00:09:01,346 --> 00:09:06,660
this is another important object oriented
concept called information hiding.

129
00:09:06,660 --> 00:09:10,550
The implementation details are hidden
inside the class.

130
00:09:10,550 --> 00:09:12,500
So, we can change those.

131
00:09:12,500 --> 00:09:15,503
We could change the data type we use to
store the

132
00:09:15,503 --> 00:09:19,276
rank of the card, and as long as we didn't
actually change

133
00:09:19,276 --> 00:09:24,358
the property, then consumers of this
class, or this object, could just keep

134
00:09:24,358 --> 00:09:30,200
using that property without even knowing
that the implementation details changed.

135
00:09:30,200 --> 00:09:31,796
And that is fantastic for

136
00:09:31,796 --> 00:09:36,736
when we actually use objects in large
software systems over time, because we

137
00:09:36,736 --> 00:09:42,070
make changes all the time when we don't
break the rest of the software.

138
00:09:42,070 --> 00:09:45,309
So this conceptual idea of an object where
we have

139
00:09:45,309 --> 00:09:48,785
the fields hidden inside and then we sort
of have this

140
00:09:48,785 --> 00:09:52,814
boundary of stuff that we expose to
people, consumers of the

141
00:09:52,814 --> 00:09:56,922
object to use, is a really powerful object
oriented idea that

142
00:09:56,922 --> 00:10:02,893
we will see again and again.
Now, I keep talking about objects and

143
00:10:02,893 --> 00:10:10,710
classes, and it's a, there's an important
distinction between the two.

144
00:10:10,710 --> 00:10:13,650
And so, let's get that distinction nailed
down right now.

145
00:10:13,650 --> 00:10:16,994
The class, is actually the template, for
creating

146
00:10:16,994 --> 00:10:20,410
objects, the class is not an object
itself.

147
00:10:20,410 --> 00:10:22,048
It's sort of the cookie cutter

148
00:10:22,048 --> 00:10:23,240
if you will.

149
00:10:23,240 --> 00:10:27,083
It tells us what all the cookies will look
like and, you know cookies tend

150
00:10:27,083 --> 00:10:29,288
not to have behavior and thus, you know,

151
00:10:29,288 --> 00:10:32,510
you have scary dreams or something about
cookies.

152
00:10:32,510 --> 00:10:34,330
But it tells us sort of the shape if

153
00:10:34,330 --> 00:10:37,444
you will, of the objects that you're going
to create.

154
00:10:37,444 --> 00:10:39,090
But it's not an object itself.

155
00:10:39,090 --> 00:10:41,050
But the good news is, it's a template.

156
00:10:41,050 --> 00:10:45,870
And we can use the class to create as many
of the actual objects as we want.

157
00:10:45,870 --> 00:10:47,146
Just as you can use a cookie

158
00:10:47,146 --> 00:10:50,050
cutter to create as many cookies as you
want.

159
00:10:50,050 --> 00:10:54,788
So it's a template that defines the state
and behavior

160
00:10:54,788 --> 00:10:59,650
of each of the objects that get created
from this class.

161
00:11:00,650 --> 00:11:02,770
The objects are the actual things.

162
00:11:02,770 --> 00:11:03,100
Right?

163
00:11:03,100 --> 00:11:08,180
They're the actual things and memory and
so we can have a whole bunch of them.

164
00:11:08,180 --> 00:11:12,330
We can have a whole bunch of playing cards
if we wanted to play cards.

165
00:11:12,330 --> 00:11:15,960
Playing cards with one card is kind of a
boring game.

166
00:11:15,960 --> 00:11:20,090
And even though each card knows, or if
the.

167
00:11:20,090 --> 00:11:21,330
Let me put it a different way.

168
00:11:21,330 --> 00:11:26,294
Even though the class says that each card
will store a rank and a suit, and whether

169
00:11:26,294 --> 00:11:28,265
or not it's face up, each of the

170
00:11:28,265 --> 00:11:33,240
individual card objects maintains their
own current information, right.

171
00:11:33,240 --> 00:11:38,280
So each of these cards has a rank and a
suit, and whether or not it's face up.

172
00:11:38,280 --> 00:11:42,360
But those ranks and suits, and face ups
can be different.

173
00:11:42,360 --> 00:11:42,530
Right?

174
00:11:42,530 --> 00:11:45,320
We can have a King of spades as well as a
Queen of hearts.

175
00:11:45,320 --> 00:11:50,720
And, they're still all cards, they're just
different instances of the cards.

176
00:11:50,720 --> 00:11:56,000
So, classes are a template, objects are
the actual things that are instances

177
00:11:56,000 --> 00:12:00,940
of, that's the terminology we use, that
are instances of that class.

178
00:12:03,480 --> 00:12:06,270
Now, when we talk about class design.

179
00:12:06,270 --> 00:12:10,020
Not actually using classes externally as
consumers of the classes.

180
00:12:10,020 --> 00:12:13,970
But actually designing classes and
understanding how they're implemented.

181
00:12:13,970 --> 00:12:17,780
We often use this notation that I've
provided here.

182
00:12:17,780 --> 00:12:20,969
And this notation includes the class name.

183
00:12:23,430 --> 00:12:24,830
The fields.

184
00:12:24,830 --> 00:12:28,370
So these are the things that we're always
going to make internal to the class.

185
00:12:30,040 --> 00:12:31,495
The properties for the class.

186
00:12:31,495 --> 00:12:35,788
So these are the things, these are the
ways that we're going to

187
00:12:35,788 --> 00:12:41,199
provide external consumers of the class to
actually find out about the state.

188
00:12:42,260 --> 00:12:43,730
And finally methods.

189
00:12:43,730 --> 00:12:46,610
That behavior stuff, that we've been
talking about.

190
00:12:47,620 --> 00:12:49,072
This notation

191
00:12:49,072 --> 00:12:57,590
is called UML and clearly, there's an in
lecture quiz about UML and what it means.

192
00:13:01,780 --> 00:13:04,167
Despite what you might have chosen in

193
00:13:04,167 --> 00:13:07,478
that quiz it stands for Unified Modelling
Language

194
00:13:07,478 --> 00:13:10,019
and it's really the most commonly used

195
00:13:10,019 --> 00:13:14,970
notation these days for specifying object
oriented systems.

196
00:13:14,970 --> 00:13:19,000
Okay, before we finish the lecture because
we're almost done we've hit

197
00:13:19,000 --> 00:13:21,145
the big ideas behind object oriented,

198
00:13:21,145 --> 00:13:24,960
implementations the whole object oriented
paradigm.

199
00:13:24,960 --> 00:13:27,144
And we'll start using it next time.
But before

200
00:13:27,144 --> 00:13:33,140
we leave, I need to point out that classes
are not value types.

201
00:13:33,140 --> 00:13:34,910
Classes are reference types.

202
00:13:34,910 --> 00:13:36,886
Remember in the previous module I said
well,

203
00:13:36,886 --> 00:13:39,480
we won't worry about reference types until
module four.

204
00:13:39,480 --> 00:13:45,520
Well, here we are in module four.
So, classes are reference typed and

205
00:13:45,520 --> 00:13:51,188
really classes are each a data type.
So, you know we had

206
00:13:51,188 --> 00:13:51,800
[INAUDIBLE]

207
00:13:51,800 --> 00:13:53,500
and float for example.

208
00:13:53,500 --> 00:13:57,950
Well, when you start implementing classes,
you're creating your very own data types.

209
00:13:57,950 --> 00:14:03,260
And they're reference types, rather than
value types, because.

210
00:14:03,260 --> 00:14:05,802
For value types, the ones and zeros at the
memory

211
00:14:05,802 --> 00:14:10,100
location for the variable represent the
value of that variable.

212
00:14:10,100 --> 00:14:13,330
For reference types, the ones and zeros

213
00:14:13,330 --> 00:14:17,035
at the memory location for the value
represent

214
00:14:17,035 --> 00:14:21,830
a reference to where the object actually
is in memory, so.

215
00:14:22,860 --> 00:14:25,848
People try not to use the word pointer,
but

216
00:14:25,848 --> 00:14:30,150
it's just a pointer to where the object
actually lives.

217
00:14:30,150 --> 00:14:31,020
So a picture might help.

218
00:14:32,380 --> 00:14:33,520
Here we go.

219
00:14:33,520 --> 00:14:37,693
So age is an integer variable, so it's a
value

220
00:14:37,693 --> 00:14:42,080
type, so the value 17 the ones and zeros
17,

221
00:14:42,080 --> 00:14:45,720
in that memory location mean ages 17.

222
00:14:45,720 --> 00:14:49,204
The rain message, which is a instance of a
message object that

223
00:14:49,204 --> 00:14:53,023
you can read about in the sorry, instance
of a message class, which

224
00:14:53,023 --> 00:14:55,837
you can read about in the book if you
choose to, but

225
00:14:55,837 --> 00:15:00,240
it just holds the message and let's you
print it and so on.

226
00:15:00,240 --> 00:15:05,050
The ones and zeros there, at that memory
location represent 44.

227
00:15:05,050 --> 00:15:07,330
But 44 is not the value.

228
00:15:07,330 --> 00:15:09,430
It's a pointer, or a reference to a

229
00:15:09,430 --> 00:15:13,460
different place in memory where the object
actually is.

230
00:15:13,460 --> 00:15:16,746
So that's that distinction between
reference types and value

231
00:15:16,746 --> 00:15:19,170
types that I said we'd get to in this
module.

232
00:15:21,510 --> 00:15:24,550
Okay, to recap the object oriented
paradigm

233
00:15:24,550 --> 00:15:27,350
not just C#, just object orientation in

234
00:15:27,350 --> 00:15:31,030
general, provides classes and objects
where classes

235
00:15:31,030 --> 00:15:34,580
serve as the template for creating
objects.

236
00:15:34,580 --> 00:15:38,719
And objects have state, behavior and
identity.

237
00:15:39,760 --> 00:15:43,225
We also learned that objects interact with
each other

238
00:15:43,225 --> 00:15:46,536
in the software that we write to actually
implement

239
00:15:46,536 --> 00:15:50,630
our game, whatever else we're doing with
our software.

240
00:15:50,630 --> 00:15:52,740
It might not be a game, how sad.

241
00:15:54,150 --> 00:15:57,222
Next time, so this was all conceptual
stuff, important

242
00:15:57,222 --> 00:16:02,190
foundational conceptual stuff to
understand how object orientation works.

243
00:16:02,190 --> 00:16:07,910
But next time, we'll actually dig in and
start using this stuff in a C# program.