Hello, in this programming video, we are going to look at modifier types. And we saw who already use them, but we saw just glance over them. Okay, so modified types are, you know, these keywords public public, and we had one a private but we renamed it. So if you don't specify keyword so like in front of the class we didn't. What this means is it's accessible to not anywhere within the same package. So there's three main sort of ones you can choose.
So I'll demonstrate so your public public means it can be so if you have a public variable, it can be accessed within the class is created. It can be accessed from child classes that are inherited from it. As you can see, we don't live equals I and you can access it outside as well. So that's probably the next lower down one is protected by four go to the character, very common to have parent classes, there aren't going to be sort of, you know, have objects created over directly for all of their properties, most of them to be protected for do like so. And we'll get errors here. And what protected does it say, the property can be accessed within the class is created, that's fine.
You can be accessed within a children class, but outside of that, so like an object is created or the class you cannot access it like so. So if for example, you see the reduce method for go to enemy, I'll change this back to public and I save it. The error disappears on reduced health. So that's the public key word. In the protected keyword, the final keyword is private. So, if I just demonstrate this or change this to private, see what happens if you have a private variable or a private method or any property, it just means it can be accessed from within the class is created and they live cannot be accessed from any children class can up indefinitely cannot be accessed from the side.
So generally speaking methods that you need to have access to you know by the object they should be public variables for the most part should even be private or protected if they need to be used for a child class, then in order to protect it, and you'd use getters and setters to it I sort of showed you in the previous video. So feel free to have a look at that. And the reason you would do this instead of just having everything public and making it easy if this book, which is a long age debate, which I'm sure you will come across in your programming career, whether it's in real life or not. StackOverflow somewhere else online in the comments on YouTube, for example, and honestly, the main reason is to help prevent you know, errors. Because if you have access to the variable anywhere, you will start accessing it, you know, anywhere you want.
And live for example, you generally can't have you know, zero lives or maybe can have zero lightning you die behind have your mind slides in, you know, a lot of games. So, if you have a getter and a setter, when you set the value, you could do some sort of check on it to see if it's a valid, you know, valid, not valid data type valid, you know, value for what that variable is supporting. What if you don't do that and says public, you or another developer on your project could just be accessing the variables assigned in the way you want. And the problem with that is, it increases the amount of places where errors can occur, if you reduce it to just you know, stuff like private and protected most of the errors that will occur Stuff like assignment most likely be within the class.
So, go to the class and look at and think okay, the problem is here. So that's it for modifier types. If you have any questions, feel free to reach out, and I look forward to seeing you in the next awesome Java video.