‫Welcome back.
‫In this video, I would like to talk about access modifiers which allow you to grant and prevent access.
‫And in order to understand access modifiers a little better and the reasoning behind them, we need
‫to look into certain concepts.
‫So first of all.
‫Marking fields and methods with access modifiers is part of the object oriented programming and increases
‫the safeness of your code and are an important part of encapsulation, which is a part of OP.
‫We will look into what this means a little more so in object oriented programming.
‫Languages such as C sharp encapsulation is used to refer to one of two related but distinct notions,
‫and sometimes to the combination thereof.
‫So a language mechanism for restricting direct access to some of the objects components such as their
‫variables and their methods, a language construct that facilitates the bundling of data with methods
‫or other functions operating on that data.
‫So for example, we can use setters and getters in order to make our whole program safer.
‫We will look into that a lot more once we go into properties which will allow us to do loads of the
‫stuff that we're going to look at in this video.
‫All right.
‫So first of all, the access modifier, private, private is only accessible inside a class or construct.
‫So if you create a private variable or a private method, it is only available within the same class
‫or structure.
‫So you cannot use it anywhere else.
‫So let's look at an example here we have class one with private int h and private void walk, which
‫is a method.
‫In this case, the method doesn't do anything, but there could be some code in there.
‫And now we have a second class, class two in which we create an object of class one, which is called
‫first class.
‫And then we want to access the edge of that first class object, which is 18.
‫But unfortunately we cannot do that because it's private.
‫So we cannot access this variable the same thing for the walk method.
‫We cannot access it because it's set as private.
‫So they are not accessible from another class.
‫They can only be used within class one.
‫Then we have the public access modifier which makes it accessible from everywhere in your project.
‫So when you use public for a variable or a method, it can be used in every single class that you have
‫within your whole project.
‫It's not limited at all.
‫Here.
‫An example again public int h and public void.
‫Walk the variable and method here and in class two we try to access them and in this case it works totally
‫fine.
‫So they are accessible from anywhere.
‫So we can of course use them in class two.
‫Then there is the access modifier called protected and that allows us to make something accessible from
‫the class and all classes that derive from it.
‫That's why it's important that you understand OP in order to or object oriented programming, in order
‫to really make use of that.
‫So we have this protected int h 18 and protected void walk.
‫So in this case we have a class two which inherits from class one and you can see it can access H now
‫it can access H directly.
‫We don't even have to create an object of our class one.
‫So that allows us to access the variable directly as if it was public.
‫So protect it is like public but only for the same class and classes that inherit from it.
‫The same goes for walk, so the walk method can be used in here easily as well and directly as well.
‫And then we have the access modifier.
‫Internal and internal variables and methods are only accessible from its assembly, so from the same
‫namespace or the same project.
‫So if you have a namespace.
‫When would you have multiple classes then?
‫All of the classes can access internal variables and internal methods.
‫So how do we use access modifiers correctly?
‫So as a rule of thumb, whenever you declare a new class member or method, I'd recommend to you initially
‫going with the most restrictive access modifier that will allow your program to work and then you can
‫relax it when necessary.
‫So make everything as private as possible.
‫And when your variables or methods should be used somewhere else, then you of course have to ease it
‫a little bit.
‫So then you could use, for example, internal, protected or even public.
‫So why should you use access modifiers?
‫Well, they give you full control over your methods and variables.
‫A very basic example would be H.
‫So let's say you want to be able to set the H, so if you would make H public, then anyone could change
‫the age variable and every class could change to h variable, even if the value doesn't make sense at
‫all.
‫So somebody could enter an H of -20 or -25 or something like that.
‫But if you control the variable via private, so if you only have setters and getters or use properties
‫as we will do later on, then you can make sure that this age variable, if it's set with a value,
‫that doesn't make sense.
‫So for example, a negative value that is just multiplied with minus one, or if it's a value, let's
‫say that is over 150 or 130 or something like that, then you would set something like zero or set something
‫like incorrect name, or you could throw an error, you could show incorrect value.
‫Enter, please enter something else.
‫Or you could just start a routine which for example, informs the right software or the right employee
‫that needs to take care of incorrect values or whatever.
‫So this is what you, as an example, can do, but there are plenty of other examples which you can
‫do, and we will look into access modifiers a lot more in the next videos.
‫So I'd say let's check out some more concepts around X modifiers, encapsulation and so forth.