1 00:00:00,500 --> 00:00:01,340 ‫Welcome back. 2 00:00:01,370 --> 00:00:07,420 ‫In this video, I would like to talk about access modifiers which allow you to grant and prevent access. 3 00:00:07,430 --> 00:00:12,590 ‫And in order to understand access modifiers a little better and the reasoning behind them, we need 4 00:00:12,590 --> 00:00:14,530 ‫to look into certain concepts. 5 00:00:14,540 --> 00:00:15,800 ‫So first of all. 6 00:00:16,890 --> 00:00:22,890 ‫Marking fields and methods with access modifiers is part of the object oriented programming and increases 7 00:00:22,890 --> 00:00:30,390 ‫the safeness of your code and are an important part of encapsulation, which is a part of OP. 8 00:00:31,110 --> 00:00:35,610 ‫We will look into what this means a little more so in object oriented programming. 9 00:00:35,610 --> 00:00:42,300 ‫Languages such as C sharp encapsulation is used to refer to one of two related but distinct notions, 10 00:00:42,300 --> 00:00:45,180 ‫and sometimes to the combination thereof. 11 00:00:45,180 --> 00:00:52,260 ‫So a language mechanism for restricting direct access to some of the objects components such as their 12 00:00:52,260 --> 00:00:58,710 ‫variables and their methods, a language construct that facilitates the bundling of data with methods 13 00:00:58,710 --> 00:01:01,890 ‫or other functions operating on that data. 14 00:01:01,890 --> 00:01:08,880 ‫So for example, we can use setters and getters in order to make our whole program safer. 15 00:01:08,880 --> 00:01:15,630 ‫We will look into that a lot more once we go into properties which will allow us to do loads of the 16 00:01:15,630 --> 00:01:17,670 ‫stuff that we're going to look at in this video. 17 00:01:17,700 --> 00:01:18,060 ‫All right. 18 00:01:18,060 --> 00:01:24,240 ‫So first of all, the access modifier, private, private is only accessible inside a class or construct. 19 00:01:24,240 --> 00:01:30,660 ‫So if you create a private variable or a private method, it is only available within the same class 20 00:01:30,660 --> 00:01:31,800 ‫or structure. 21 00:01:31,800 --> 00:01:33,570 ‫So you cannot use it anywhere else. 22 00:01:33,570 --> 00:01:39,360 ‫So let's look at an example here we have class one with private int h and private void walk, which 23 00:01:39,360 --> 00:01:40,140 ‫is a method. 24 00:01:40,140 --> 00:01:43,740 ‫In this case, the method doesn't do anything, but there could be some code in there. 25 00:01:43,740 --> 00:01:49,950 ‫And now we have a second class, class two in which we create an object of class one, which is called 26 00:01:49,950 --> 00:01:51,120 ‫first class. 27 00:01:51,120 --> 00:01:57,120 ‫And then we want to access the edge of that first class object, which is 18. 28 00:01:57,120 --> 00:02:00,690 ‫But unfortunately we cannot do that because it's private. 29 00:02:00,690 --> 00:02:05,310 ‫So we cannot access this variable the same thing for the walk method. 30 00:02:05,310 --> 00:02:08,220 ‫We cannot access it because it's set as private. 31 00:02:08,490 --> 00:02:11,250 ‫So they are not accessible from another class. 32 00:02:11,250 --> 00:02:13,320 ‫They can only be used within class one. 33 00:02:13,530 --> 00:02:19,350 ‫Then we have the public access modifier which makes it accessible from everywhere in your project. 34 00:02:19,350 --> 00:02:26,160 ‫So when you use public for a variable or a method, it can be used in every single class that you have 35 00:02:26,160 --> 00:02:27,810 ‫within your whole project. 36 00:02:27,930 --> 00:02:29,250 ‫It's not limited at all. 37 00:02:29,400 --> 00:02:29,790 ‫Here. 38 00:02:29,790 --> 00:02:33,810 ‫An example again public int h and public void. 39 00:02:33,810 --> 00:02:39,960 ‫Walk the variable and method here and in class two we try to access them and in this case it works totally 40 00:02:39,960 --> 00:02:40,320 ‫fine. 41 00:02:40,320 --> 00:02:42,630 ‫So they are accessible from anywhere. 42 00:02:42,630 --> 00:02:44,820 ‫So we can of course use them in class two. 43 00:02:45,540 --> 00:02:51,420 ‫Then there is the access modifier called protected and that allows us to make something accessible from 44 00:02:51,420 --> 00:02:54,780 ‫the class and all classes that derive from it. 45 00:02:54,960 --> 00:03:00,600 ‫That's why it's important that you understand OP in order to or object oriented programming, in order 46 00:03:00,600 --> 00:03:02,520 ‫to really make use of that. 47 00:03:02,520 --> 00:03:07,620 ‫So we have this protected int h 18 and protected void walk. 48 00:03:07,620 --> 00:03:17,730 ‫So in this case we have a class two which inherits from class one and you can see it can access H now 49 00:03:17,850 --> 00:03:19,380 ‫it can access H directly. 50 00:03:19,380 --> 00:03:23,880 ‫We don't even have to create an object of our class one. 51 00:03:24,090 --> 00:03:29,700 ‫So that allows us to access the variable directly as if it was public. 52 00:03:30,630 --> 00:03:36,840 ‫So protect it is like public but only for the same class and classes that inherit from it. 53 00:03:36,840 --> 00:03:45,090 ‫The same goes for walk, so the walk method can be used in here easily as well and directly as well. 54 00:03:45,720 --> 00:03:47,280 ‫And then we have the access modifier. 55 00:03:47,280 --> 00:03:54,210 ‫Internal and internal variables and methods are only accessible from its assembly, so from the same 56 00:03:54,210 --> 00:03:56,730 ‫namespace or the same project. 57 00:03:56,730 --> 00:03:58,380 ‫So if you have a namespace. 58 00:03:59,120 --> 00:04:01,670 ‫When would you have multiple classes then? 59 00:04:01,670 --> 00:04:07,520 ‫All of the classes can access internal variables and internal methods. 60 00:04:08,300 --> 00:04:13,410 ‫So how do we use access modifiers correctly? 61 00:04:13,430 --> 00:04:19,910 ‫So as a rule of thumb, whenever you declare a new class member or method, I'd recommend to you initially 62 00:04:19,940 --> 00:04:26,030 ‫going with the most restrictive access modifier that will allow your program to work and then you can 63 00:04:26,030 --> 00:04:28,400 ‫relax it when necessary. 64 00:04:28,400 --> 00:04:31,790 ‫So make everything as private as possible. 65 00:04:31,790 --> 00:04:37,970 ‫And when your variables or methods should be used somewhere else, then you of course have to ease it 66 00:04:37,970 --> 00:04:38,390 ‫a little bit. 67 00:04:38,390 --> 00:04:43,460 ‫So then you could use, for example, internal, protected or even public. 68 00:04:43,940 --> 00:04:48,000 ‫So why should you use access modifiers? 69 00:04:48,020 --> 00:04:51,530 ‫Well, they give you full control over your methods and variables. 70 00:04:51,710 --> 00:04:53,660 ‫A very basic example would be H. 71 00:04:53,660 --> 00:05:02,690 ‫So let's say you want to be able to set the H, so if you would make H public, then anyone could change 72 00:05:02,690 --> 00:05:08,810 ‫the age variable and every class could change to h variable, even if the value doesn't make sense at 73 00:05:08,810 --> 00:05:09,110 ‫all. 74 00:05:09,110 --> 00:05:14,780 ‫So somebody could enter an H of -20 or -25 or something like that. 75 00:05:15,020 --> 00:05:23,150 ‫But if you control the variable via private, so if you only have setters and getters or use properties 76 00:05:23,150 --> 00:05:30,590 ‫as we will do later on, then you can make sure that this age variable, if it's set with a value, 77 00:05:30,590 --> 00:05:31,460 ‫that doesn't make sense. 78 00:05:31,460 --> 00:05:38,780 ‫So for example, a negative value that is just multiplied with minus one, or if it's a value, let's 79 00:05:38,780 --> 00:05:47,960 ‫say that is over 150 or 130 or something like that, then you would set something like zero or set something 80 00:05:47,960 --> 00:05:52,700 ‫like incorrect name, or you could throw an error, you could show incorrect value. 81 00:05:52,700 --> 00:05:54,500 ‫Enter, please enter something else. 82 00:05:54,500 --> 00:06:01,850 ‫Or you could just start a routine which for example, informs the right software or the right employee 83 00:06:01,850 --> 00:06:04,820 ‫that needs to take care of incorrect values or whatever. 84 00:06:04,820 --> 00:06:09,950 ‫So this is what you, as an example, can do, but there are plenty of other examples which you can 85 00:06:09,950 --> 00:06:14,410 ‫do, and we will look into access modifiers a lot more in the next videos. 86 00:06:14,420 --> 00:06:20,870 ‫So I'd say let's check out some more concepts around X modifiers, encapsulation and so forth.