1
00:00:00,830 --> 00:00:04,580
Now that we've seen how methods work inside of classes, we're going to take a look at how we handled

2
00:00:04,580 --> 00:00:06,990
data inside them as well to get started.

3
00:00:07,010 --> 00:00:11,900
I'm going to clean up this air right here around vehicle honk by deleting that line, and I'll do the

4
00:00:11,900 --> 00:00:13,540
same thing at the bottom of this file as well.

5
00:00:15,880 --> 00:00:22,090
All right, it's now back at the top on vehicle, I want to add in to fields, these are essentially

6
00:00:22,090 --> 00:00:24,850
going to be properties are going to reference actual values.

7
00:00:26,040 --> 00:00:30,390
Whenever you want to specify a property inside of a class, we'll just write out the name of the property,

8
00:00:30,390 --> 00:00:34,860
so maybe color and then a type invitation for that property.

9
00:00:35,190 --> 00:00:38,220
So in our case, maybe a color should be a string like so.

10
00:00:39,250 --> 00:00:44,140
And right now, as soon as you type out this line of code, you might see an air around this line,

11
00:00:44,350 --> 00:00:45,580
specifically around color.

12
00:00:46,090 --> 00:00:49,030
If you don't see an air in your screen, looks like mine, that's totally fine.

13
00:00:49,600 --> 00:00:54,880
If you do see and there it is, because traditionally we want to initialize a variable any time we declare

14
00:00:54,880 --> 00:00:55,960
it inside of a class.

15
00:00:56,620 --> 00:01:02,140
So right now we have declared the existence of a property or a field attached to class vehicle, but

16
00:01:02,140 --> 00:01:08,890
we have not give it and given it an initial value to do so right after the class or the property definition.

17
00:01:08,890 --> 00:01:11,920
We can write in an equal sign and then some value.

18
00:01:12,010 --> 00:01:13,870
In my case I'll say read like so.

19
00:01:14,760 --> 00:01:20,400
So this means that every time we create an instance of class vehicle, the instance will have a color

20
00:01:20,400 --> 00:01:24,540
property that is a string and its starting value will be equal to red.

21
00:01:25,350 --> 00:01:28,740
So, for example, down here, right underneath CONSED vehicle is new vehicle.

22
00:01:28,740 --> 00:01:33,090
We could do a console log of vehicle, not color like so.

23
00:01:33,900 --> 00:01:39,360
If we now save this, look back over to our terminal one more time and run that file.

24
00:01:41,820 --> 00:01:43,560
We'll see a council log of red up here.

25
00:01:44,470 --> 00:01:47,960
Now, this is just one way to initialize a variable inside of a class.

26
00:01:48,460 --> 00:01:53,440
It's also very common to want to initialize a variable using some configuration options that we passed

27
00:01:53,440 --> 00:01:55,460
to the class when we create an instance out of it.

28
00:01:56,110 --> 00:02:00,940
So, for example, it'd be a lot nicer to specify the color, right, when we create a vehicle, because

29
00:02:00,940 --> 00:02:02,920
sometimes a vehicle might have a color of red.

30
00:02:03,220 --> 00:02:06,880
Other times it might be silver or black or orange or whatever it might be.

31
00:02:07,630 --> 00:02:12,000
So to do so, we could pass in the color as an argument to vehicle right here.

32
00:02:12,430 --> 00:02:14,260
So maybe I'll do something like orange.

33
00:02:15,670 --> 00:02:20,980
If we're going to pass in any arguments to this, we have to define a special function inside of the

34
00:02:20,980 --> 00:02:23,380
class definition called the constructor.

35
00:02:25,340 --> 00:02:30,140
So I'm going to write out constructor like so the constructor function is a very special function defined

36
00:02:30,140 --> 00:02:36,770
inside of a class, any time that we define a constructor, it will be instantly executed, right when

37
00:02:36,770 --> 00:02:38,500
we create a new instance of the class.

38
00:02:38,510 --> 00:02:43,820
So as soon as we call new vehicle right here, the constructor function is going to run and the arguments

39
00:02:43,820 --> 00:02:47,910
that function are going to be whatever we pass in when we create the instance.

40
00:02:47,930 --> 00:02:50,030
So in this case would be a string of orange.

41
00:02:51,510 --> 00:02:56,370
So we would probably want to receive that as an argument into the constructor of something like maybe

42
00:02:56,370 --> 00:02:58,440
color with an annotation of string like so.

43
00:02:59,470 --> 00:03:04,810
And then we'll take that value and do some initial assignment with it inside the constructor, so we

44
00:03:04,810 --> 00:03:07,240
would say this color is equal to color.

45
00:03:08,500 --> 00:03:13,660
If we use this method of initializing color by defining the constructor, we do not have to initialize

46
00:03:13,660 --> 00:03:14,280
it up here.

47
00:03:14,770 --> 00:03:16,740
So it's usually an either or decision.

48
00:03:17,110 --> 00:03:22,600
Either you are going to initialize a property on the same line that you define it or you're going to

49
00:03:22,600 --> 00:03:24,310
initialize it inside the constructor.

50
00:03:24,310 --> 00:03:28,420
One or the other in this case will stick with the constructor method.

51
00:03:28,480 --> 00:03:29,920
So I'm going to delete the equals.

52
00:03:29,920 --> 00:03:30,730
Read up here.

53
00:03:32,460 --> 00:03:38,340
So now if I save this file again, I can run it in my terminal and I should now see I got a little air

54
00:03:38,340 --> 00:03:38,640
here.

55
00:03:38,940 --> 00:03:39,510
Let's see.

56
00:03:39,690 --> 00:03:41,780
Argument for color was not provided by mistake.

57
00:03:42,360 --> 00:03:42,950
Let's see.

58
00:03:42,960 --> 00:03:44,010
Oh, I know what it is.

59
00:03:44,460 --> 00:03:46,260
We created a car down here as well.

60
00:03:46,290 --> 00:03:48,720
Let's just comment out all this stuff down here really quickly.

61
00:03:49,760 --> 00:03:54,260
While we were working on that first class, this actually is a very valuable subject that we have to

62
00:03:54,260 --> 00:03:58,910
discuss, like what we do with constructor's in extended classes, but let's just hold off on that for

63
00:03:58,910 --> 00:03:59,360
right now.

64
00:03:59,900 --> 00:04:02,420
So I just commented out everything in the second half to file.

65
00:04:02,570 --> 00:04:04,570
I'm going to save the file and then try to run it again.

66
00:04:05,270 --> 00:04:05,940
And there we go.

67
00:04:05,960 --> 00:04:06,800
Now I see orange.

68
00:04:06,800 --> 00:04:07,280
Perfect.

69
00:04:08,440 --> 00:04:14,080
Now, just you know, there's also a little shortcut to automate this entire process right here to notice

70
00:04:14,080 --> 00:04:17,649
how we kind of have to refer to these repetitive properties multiple times.

71
00:04:17,649 --> 00:04:20,589
I see color, color, color all over the place.

72
00:04:21,399 --> 00:04:26,620
A equivalent way to write all this out by using a little shortcut would be to delete the implementation

73
00:04:26,620 --> 00:04:27,670
inside the constructor.

74
00:04:28,580 --> 00:04:30,740
Delete the property definition at the top.

75
00:04:32,420 --> 00:04:37,220
And then right in front of the color argument, we could add on the word public like so.

76
00:04:38,160 --> 00:04:43,500
Public is one of those modifiers we spoke about, these modifiers can be used not only on methods in

77
00:04:43,500 --> 00:04:46,020
a class, but on properties or fields as well.

78
00:04:46,940 --> 00:04:51,470
So this syntax you see right here is going to mean that we're going to take whatever comes in as the

79
00:04:51,470 --> 00:04:56,660
first argument to that constructor, which is in this case, it's orange and it will be assigned, as

80
00:04:56,660 --> 00:05:00,130
an instance, variable of color to our vehicle that we are creating.

81
00:05:00,950 --> 00:05:05,780
So the code that you see right here is 100 percent equivalent to this right here.

82
00:05:06,320 --> 00:05:07,730
It's just slightly shortened up.

83
00:05:07,730 --> 00:05:11,360
So we don't have to refer to the same color, color, color all over the place.

84
00:05:12,470 --> 00:05:17,240
So we're going to use this shortcut many times in the code that we write, if we do something like this,

85
00:05:17,240 --> 00:05:20,450
we can then shorten up that body of the constructor function as well.

86
00:05:20,480 --> 00:05:22,490
Even though it's empty, we still have to put it in.

87
00:05:23,510 --> 00:05:28,310
So let's try saving this flip back over, run the file again, and we will see orange again.

88
00:05:31,190 --> 00:05:36,290
All right, now, those modifiers we spoke about apply to variables just the same way as they do to

89
00:05:36,290 --> 00:05:42,700
methods so we could alternatively use the private keyword or the private modifier on color.

90
00:05:43,280 --> 00:05:47,570
If we do so, we will no longer be able to read that variable outside of our class anymore.

91
00:05:48,470 --> 00:05:51,100
Likewise, we could also mark this thing as being protected.

92
00:05:51,740 --> 00:05:57,500
We still cannot access it outside the class, but we would be able to access that field from a class

93
00:05:57,500 --> 00:06:00,770
that extends vehicle such as car down here.

94
00:06:02,340 --> 00:06:07,290
All right, I'm going to leave that variable as public like so and I'll say the file, we're going to

95
00:06:07,290 --> 00:06:08,040
leave it like this.

96
00:06:08,220 --> 00:06:09,470
Let's take a quick pause right here.

97
00:06:09,510 --> 00:06:14,970
We're going to come back the next video and talk about how these fields work when we are using extensions

98
00:06:14,970 --> 00:06:15,840
like we are down here.

99
00:06:16,140 --> 00:06:18,020
So quick pause and I'll see you in just a minute.

