Java Access Modifiers - Learn Public, Private, Protected and Default

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Java access modifiers the three most boring words in the English language I'm gonna show you what Java access modifiers are today and they're actually not as bad as you think anyway sup I'm Alex welcome back to my channel if you're new here I make a Java tutorial just like this one every single week I've got like 3040 Java tutorials just like this one on my channel so if you're new here and you might be interested in seeing a new one every week then consider subscribing so first we're in Eclipse I'm just gonna go to file new Java project to start it off I'll call it like access fun hit finish open up and we're gonna create two packages here so on the source folder go to a new package and we'll call this like package one right click on source again and go to new package again and we'll call it package two and this is all about setting up and learning what these access modifiers are gonna do for us so in package one we'll go to a new class don't call it access one make another one called access to say access to and then hit this public static void main checkbox hit finish and the last one in the second package go to file in their class and we'll call this one access 3 just like that close out access 3 and we'll just look at 1 & 2 right now so right now this Java file in this Java file look a little different this one has this main method in here and this one doesn't the only reason we're doing that is so that we can see what these access modifiers are gonna do for us say access 1 is gonna act like a clock it has some data it's got hours it's got minutes but all that information is practically useless if you can't share it between other files access modifiers let us give access to other files about the data so we can create some integers here say hours and minutes say it's ours equals say three and we've got some minutes here say equal to 47 there are four types of Java access modifiers the first one I'll just write a comment is default the second one is public the third one is private and the last one is protected each of these changes access four different files but we're going to start at default the default is what happens when you don't do anything and since we did in two hours here and needs minutes here without doing anything else it's using the default access modifier so if we save it and try to access it through access to by creating the access one object so say access one a equals new access one just like this we can get the data the hours and minutes from this access one file by now just typing a dots and then you'll see hours and minutes you'll also see these green circles these are just methods that are built into every Java class so these are nothing special but we do see these hours and minutes from this file so I can just type hours now and print that to the screen and we should get three yeah and we've got that three there if I copy this and put the minutes on minutes then we'll get three and forty-seven the data from this file and that's what the default access modifier that lets everyone see all data there aren't any restrictions everyone can see it this is default now let's move on to public to use these other three access modifiers you have to put them in front so for default we didn't put anything in front but for the other access modifiers we have to put them in front so we'll just say public for each now if we save and run it it does the exact same thing if we remove these and then put that dot again to bring it up we see the hour minute hours and minutes again except now that leads little green circles they weren't blue triangles like before the blue triangle means a default access modifier and this green circle is the public might be different on different IDs but in Eclipse this is how I do it what you need to know about public is that it's pretty much the same so the only difference between default and public is that public you write it in front it sort of helps you keep track and this works the same across packages so access one and access to these two files were in the same package or folder if we go into this other folder package two we can do the same thing so I can just copy and paste that over notice how it auto-generated this import statement going into package one and the file access one to bring that code into this program if we save and run this then we'll get three and forty-seven even though they're in different packages so we went over default we went over public and now let's do private so let's change the use of private and see what happens as soon as we did that we got some errors in the other two files this is because they no longer have access we change the access from public everyone can see it to private only access one knows about hours and minutes this doesn't even know that they exist so if we do dots we don't see any hours or minutes in here so that's what private does it keeps it private - just this file lastly we have protected so if I change this to protected protected and go to access to if we do a dots then we see hours and minutes again except now they have little a little yellow diamond if we click it in two hours and minutes save and run it oh I'll fix some errors in this one let's delete that we run access to then we still get the minutes and hours and that's because they're in the same package if it's protected it gives access to everything in the same package but if it's in a different package then it doesn't have access so if I do a dot in the access three file which is under different package completely it doesn't even know about it but if I do a dot again here a dot ours it does so protected changes access based on what package you're on but there's also one very important part about protected it only lets other packages know about the data inside of it if it's related to the original class so if X s3 is related to access one then it should be able to see it another way of saying this is if X s3 extends access 1 this means if X s3 is a subclass of the original I'm not going to talk about that too much today but it just basically means they're related if I now do access one it still gives errors because we can't get the original still it has to be accessed through the subclass so if I change this to access 3 then everything is fine and dandy and we can do this same thing inside of access to now and get access 1 and access 3 you know something different then it does both I'll just make this a little less confusing now I know a lot and this works for methods as well so if we have a public method that gets ours get ours and it returns the hours then in access to we can instead do a dots get ours now and that will call that coat and return the number of hours just a different way if we did nothing then that's the default access modifier and it will act just like public if we do privates if this is a private method then we can't we don't have access to that anymore but if it's protected protected then we have access to it because it's in the same package but if we try to do this inside of access 3 here change that to 3 then we can even though it's in an other package because access 3 is related to access 1 by doing this extends it'll run it yeah so go over all the Java access modifiers one more time we've got defaults which is nothing and that's basically public everything can see it I'm sorry I forgot to mention this defaults gives access to everything in that package but not to the other packages so you can see now since we don't have anything in front it's the default it won't let package to see any of it but if we change each of these to public public public and now even things outside of the package can see it so default is accessed within the package public is access everywhere across all the packages private is only the file so I can do privates will do private together change all these to private and now nothing else can see it and protected the last one gives access within the package and if it's accessed through a subclass by doing extends the original so I hope this was helpful on Java access modifiers if it was please leave a like share with a friend if you think it might help them I know I struggled a lot with these so might be useful I appreciate you watching and I'll see you in the next video [Music]
Info
Channel: Alex Lee
Views: 102,339
Rating: 4.9540782 out of 5
Keywords: java access modifiers, java access modifiers tutorial, access modifiers in java, access modifiers, access modifiers java, access modifiers tutorial, public private protected, public private protected in java, java public private protected, public private protected default in java, learn java, java
Id: H0OetoieSDQ
Channel Id: undefined
Length: 11min 27sec (687 seconds)
Published: Thu Jul 04 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.