11.5 Access Modifier

Cliquez içi pour plus de vidéos disponibles sur notre chaine youtube !

We’re going to talk about access modifiers. In java, the application of the encapsulation concept is possible in part thanks to the access modifiers. They allow us to define the visibility, portability and integrity of the data processed. The best way to understand better is to make examples. We will create two packages. I have already created one and I will create a second one. In package 1, I will create a class that will be "access 1 " and in the 2nd package I will also create a class that will be "access 2" Access 1 will act as a clock, so it will have data like the times, minutes but this information is practically useless if you can’t share it among other files. So the access modifiers will allow us access to give us other files. In "access 1", we will create two variables int hours = 3 and int minutes = 47 In java, there are four access levels called Default public private Et protected We will start with "default" So, default is what happens when you don’t do anything like time and minutes here, we just declared "int hours" and "int minutes" nothing special so this is the default access modifier. We record it and try to access it by creating the Access 1 object in the main class. it will be access 1 a = new access 1 If we do a. And as we can see, we can access our variables. And this is what the default access modifier It allows everyone to see all the data there are no restrictions everyone can see, it’s by default. Now let’s go public. You have to put it in front, so by default we don’t put anything in front and it’s the same for others we have to put it in front and if we record it and run the program it does exactly the same thing. So the only difference between "default" and "public" is that you write it in front and it kind of helps you keep track and it works the same way in all packages. I will copy all these codes and put it in access 2 which will allow us to do the private test and protected it automatically generated the import of package1 and access 1. so if we save and execute this we will get 3 and 47 even if they are in different packages. So, we’re gonna change public to "private" and see what happens as soon as we do. We had errors in both files because they no longer have access. We changed the "public" access that everyone could see in private access only In the hand, if I do right we no longer see our variables that’s what "private" does so for access, you have to create getters and setters I go back to access 1. And I come here I will create a getter of the hour for example. And now normally we can access the time. So if I do a. get time And I restart the program it should get us out 3 Now if I do "protected " In the main class if I try to access by doing a. Then we see our variables again. Hour and minutes And if we relaunch our program We always get the minutes and time because they are in the same package. So if it is protected, it gives access to everything in the same package but if it is in a different package there is no access. Protected access changes depending on the package you are on That’s all for the access modifiers I hope you have understood more or less. Come on! I’ll see you next time.