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