1
00:00:00,890 --> 00:00:05,270
In the last two videos, we spoke about some basic topics around in classes that for the most part are

2
00:00:05,270 --> 00:00:09,680
probably going to be at least a little bit familiar if you have covered 2015 before.

3
00:00:10,100 --> 00:00:15,680
So now we're going to start to discuss some of the differences between 2015 classes and classes in TypeScript.

4
00:00:16,290 --> 00:00:20,390
So the first thing we're going to cover is the existence of something called modifiers.

5
00:00:21,590 --> 00:00:26,990
Modifiers are keywords that we can place on different methods and properties inside of a class, the

6
00:00:26,990 --> 00:00:29,500
different options are public, private and protected.

7
00:00:30,260 --> 00:00:35,360
The goal of these different modifiers is to restrict access to different functions or different variables

8
00:00:36,080 --> 00:00:36,820
by default.

9
00:00:36,830 --> 00:00:42,190
Every different method and property that we add to a class is always going to have the public, simply

10
00:00:42,200 --> 00:00:43,490
the public modifier on it.

11
00:00:44,030 --> 00:00:48,500
Now, the reason I say by default is that right now you'll notice we don't have anything that looks

12
00:00:48,500 --> 00:00:51,110
like public, private or protected inside of our class.

13
00:00:51,710 --> 00:00:57,110
So because we did not add anything or any of those modifiers inside of here, it is assumed that these

14
00:00:57,110 --> 00:00:58,850
are all currently public.

15
00:00:59,620 --> 00:01:01,100
So what does it mean to actually be public?

16
00:01:01,700 --> 00:01:05,990
Well, it means that a given method or property, because we can add a modifier on a property as well,

17
00:01:05,990 --> 00:01:07,310
but we'll discuss that in a little bit.

18
00:01:07,790 --> 00:01:11,090
It means that method can be called anywhere, any time.

19
00:01:12,300 --> 00:01:17,820
It's easiest to understand what public means if we then look first at private and protected, if a method

20
00:01:17,820 --> 00:01:23,580
is marked as private, it means that that method can only be called by other methods in the exact same

21
00:01:23,580 --> 00:01:24,630
class definition.

22
00:01:25,770 --> 00:01:31,050
If a method is marked as protected, it means it can be called by other methods in that class and by

23
00:01:31,050 --> 00:01:33,510
other methods in child classes as well.

24
00:01:34,960 --> 00:01:39,250
By far, the easiest way to understand all this is to just write out code samples, so let's do that

25
00:01:39,250 --> 00:01:39,670
right now.

26
00:01:41,410 --> 00:01:46,630
So right now, back inside of my editor, I'm going to add on the public modifier to all three of these

27
00:01:46,630 --> 00:01:50,200
different methods, you see, I'm going to say public, public.

28
00:01:52,550 --> 00:01:58,100
By doing so, I have changed absolutely nothing inside of my code, again, by default, all these different

29
00:01:58,100 --> 00:02:00,560
modifiers are considered to be public.

30
00:02:01,640 --> 00:02:06,560
So in general, it's only when we start to add on different modifiers like protected or private, that

31
00:02:06,560 --> 00:02:07,890
we're going to see any difference.

32
00:02:08,750 --> 00:02:12,740
Let's try marking the drive method right here as private to get started.

33
00:02:13,610 --> 00:02:15,680
So I'm going to add on the private modifier to that.

34
00:02:16,620 --> 00:02:22,050
As soon as I do so, you're going to see an air appear on car drive, it says property drive is private

35
00:02:22,050 --> 00:02:24,240
and only accessible within class car.

36
00:02:24,900 --> 00:02:29,400
So that means that we can never call this method from outside of this class.

37
00:02:29,940 --> 00:02:32,690
Only methods inside this class can actually call drive.

38
00:02:33,480 --> 00:02:38,730
So let's say, for example, that we had a new method to this thing called start driving process.

39
00:02:40,690 --> 00:02:45,490
And I'm going to have that with a return type, avoid this method right here, because it is inside

40
00:02:45,490 --> 00:02:50,740
of a car, can safely call the drive method by referring to this drive like so.

41
00:02:52,070 --> 00:02:57,090
So now down here on Khadar Drive, rather than calling car drive, we could instead call start driving

42
00:02:57,110 --> 00:02:57,860
process.

43
00:02:58,550 --> 00:03:02,930
We can safely call start driving process because it does not have the private modifier on it.

44
00:03:03,990 --> 00:03:10,290
And start driving process can safely call, drive, even though drive is marked as private because is

45
00:03:10,290 --> 00:03:12,450
an inside of the same class.

46
00:03:13,890 --> 00:03:16,790
You'll notice we've got a little air right here if you hover over it.

47
00:03:18,170 --> 00:03:24,020
We're seeing this air because we have added in a new property or we've modified a property in class

48
00:03:24,020 --> 00:03:27,680
car that is also existing inside a vehicle as well.

49
00:03:28,280 --> 00:03:33,110
But in vehicle, our definition up here drive is marked as public.

50
00:03:33,920 --> 00:03:38,480
So the air here is that we've got a child class that has a different modifier than the parent class.

51
00:03:39,350 --> 00:03:45,290
If we ever are overriding a method, as we are right here, we cannot change the modifier in the child

52
00:03:45,290 --> 00:03:45,800
class.

53
00:03:46,550 --> 00:03:49,970
So in this example, if we remove private life so that air will go away.

54
00:03:51,230 --> 00:03:56,550
However, if vehicle did not have that method at all, we are no longer overriding it inside of car.

55
00:03:56,660 --> 00:04:00,260
And so now we can successfully add on private like so.

56
00:04:01,880 --> 00:04:05,660
OK, so, again, we're going to use private whenever we want to restrict access to a function.

57
00:04:06,290 --> 00:04:09,380
Now, here's the really important thing, like why do we care about this?

58
00:04:09,380 --> 00:04:09,570
Right.

59
00:04:09,590 --> 00:04:11,160
That's the real important definition.

60
00:04:11,690 --> 00:04:16,220
The first thing I want to tell you and make extremely clear is that we do not mark Method's as being

61
00:04:16,220 --> 00:04:22,730
private over any type of security concern around like being hacked or something like that or having

62
00:04:22,730 --> 00:04:23,900
some malicious user.

63
00:04:24,800 --> 00:04:30,870
We need to find a method as being private, we are not adding in any layer of application security.

64
00:04:31,580 --> 00:04:36,680
The only reason to mark something as private is to restrict the different methods that other developers

65
00:04:36,680 --> 00:04:37,280
can call.

66
00:04:37,800 --> 00:04:42,740
And the reason we want to do that is if we might have a method that very deeply manipulates a class

67
00:04:42,860 --> 00:04:45,980
and maybe there's some, like, really complicated special way to use it.

68
00:04:46,460 --> 00:04:50,170
And maybe you and I don't trust other developers to call that method safely.

69
00:04:50,780 --> 00:04:56,040
If other developers call a given method, they might just inadvertently break something inside of application.

70
00:04:56,750 --> 00:05:00,950
And so if that's the case, if we have some method that we just don't want other developers to call

71
00:05:01,070 --> 00:05:05,150
because it's very likely they're going to break something, we will mark it as being private.

72
00:05:07,210 --> 00:05:13,240
OK, now we've taken a look at private, let's also discuss what protected does, so protecting is just

73
00:05:13,240 --> 00:05:13,730
like private.

74
00:05:13,750 --> 00:05:19,180
The only difference is that with protected, we can access the given method in child classes as well.

75
00:05:20,360 --> 00:05:25,880
So, for example, let's go back up to vehicle right here, rather than saying that Honk is going to

76
00:05:25,880 --> 00:05:28,910
be public, let's make it as private really quickly.

77
00:05:29,730 --> 00:05:33,870
And then let's say that when we start the driving process, we're not only going to do a drive, but

78
00:05:33,870 --> 00:05:35,130
we'll also do a honk.

79
00:05:38,660 --> 00:05:44,960
So when we try to call Huncke, our class is going to automatically try to call this method, but because

80
00:05:44,960 --> 00:05:49,640
this thing is marked as private and is in a parent class, we are not allowed to do so.

81
00:05:50,470 --> 00:05:53,050
So is only accessible within vehicle.

82
00:05:54,530 --> 00:05:58,820
If we still actually really wanted to call honk, even though it's inside of a parent class, we could

83
00:05:58,820 --> 00:06:00,110
mark it as protected.

84
00:06:01,060 --> 00:06:06,490
So now that child class can access that method, but it cannot be accessed by anywhere outside of that

85
00:06:06,490 --> 00:06:07,280
child class.

86
00:06:08,140 --> 00:06:13,450
So, for example, if we now made an instance of vehicle right here, so consed vehicle.

87
00:06:15,330 --> 00:06:16,110
His new vehicle.

88
00:06:17,650 --> 00:06:19,690
We would not be allowed to call huncke.

89
00:06:20,590 --> 00:06:23,620
If I try to do so well, very quickly, I see an error message once again.

90
00:06:25,120 --> 00:06:31,150
OK, so that's the basics of modifiers, so we've now taken a look at basics of classes, methods,

91
00:06:31,300 --> 00:06:34,840
a very tiny bit around inheritance and modifiers as well.

92
00:06:35,200 --> 00:06:36,130
So quick pause right here.

93
00:06:36,190 --> 00:06:38,530
We're going to start to cover fields in the next section.

94
00:06:38,560 --> 00:06:39,960
So I'll see you in just a minute.

