Welcome back.
In this video, I wanted to talk about two access modifiers that we have used.
And I still haven't talked about them because I just thought to talk about all of the other things before
we go over it.
I have, however, explained how it works, generally speaking, but I just wanted to show you the two
keywords, private and public in a particular example so that you get a better understanding of those
two keywords.
So the thing is, there are more access modifiers, which private and public are so internal, by the
way, is also an access modifier.
So access modifier internal okay.
But we have used them pretty much already right.
We have used public and we've used private.
So public is something if you want to make it available outside of the class and private if you don't
want to make it available outside of the class.
So we can access the number of cars, but we cannot access brand from our program class.
So here you can see I can use number of cars because it's a public static variable if I were to make
it private however.
So for example private it could only be used inside of this particular class car.
Outside of it.
It would not be available.
So it says is inaccessible due to its protection level.
So we increased the protection level from public to private.
Now there are more protection levels such as protected and internal and protected internal, but we're
not going to need those until we looked at inheritance, which is a topic that we are going to cover
later on.
But for now, just be aware that you need to make sure that you make your properties public so that
you can access them from the outside unless you don't want to.
But usually you would always make your properties public and your member variables or backing fields
private.
And then with methods where you want the methods to only be available inside of a given class, you
would make it private.
And if you want it to be available from all classes, you would make it public.
And usually what you would do is you make a method public.
So you you add the public keyword to your method like here void drive.
And you make your member variables private.
That's pretty much what I have to say about it.
Now let's have a look at our example here.
So by the way, it wouldn't make sense to make this static number of cars private because then we could
only access it from the car itself.
But we really want to access it from our program file.
So I'm going to make that public again.
But if we look at our drive method here, right.
This drive method, if I were to make drive.
So let's say car three dot drive, if I were to make that private, I cannot run this line of code anymore.
But let's run it for now.
Let's see how it does.
And it says I'm an A3 and I'm driving.
Now, if I go into the car, you can see here that's because it's public.
And by the way, it added a reference because it says, hey, this method is used.
So now we can see that it says, okay, there's this reference inside of class app slash program CSS.
And then in line 11 it uses the statement car dot drive.
Okay cool.
So if I were to make this private however I could only use drive from let's say my constructor here
or any other methods that are inside of my car class, but I cannot use it from outside of the car class
anymore because it's private now.
So you can see here I get this error.
It says inaccessible due to its protection level.
So now I cannot even run this line because it directly says there is an error.
So it won't accept this anymore.
So why would you even bother?
Well, the practical use is that access modifiers are crucial for managing how classes and class members
are accessed in software projects.
So by controlling access, you can safeguard the internal state of objects, reduce dependencies between
components and enhance the maintainability.
So basically how easy it is to work in the on the project in the future and add new features to the
project.
So yeah, that would be pretty much it for this video.
See you in the next one.