
Java access modifiers: (public, protected, private) 🔒
Java access modifiers: (public, protected, private) 🔒
Java access modifiers public protected private
#java #public #private #protected
//
public: visible in all classes in all packages
protected: visible to all classes in the same package or classes in other packages that are a subclass
default: visible to all classes in the same package
private: visible only in the same class
//
Content
0 -> hey what's going on everybody it's your
1.52 -> bro hope you're doing well and in this
3.28 -> video i'm going to teach you guys about
4.799 -> different access modifiers in java so
7.12 -> sit back
7.759 -> relax and enjoy the show
11.679 -> if you find this video helpful please
13.28 -> remember to like
14.799 -> comment and subscribe your support will
17.119 -> help keep this channel running
19.279 -> all right everyone so axis modifiers add
22.32 -> a layer of security to our programs and
24.88 -> there are three
25.84 -> axis modifiers i'm going to cover
27.92 -> besides the default which is none
30 -> they are public protected and
33.12 -> private so to best demonstrate this
35.28 -> we're going to need some packages
37.2 -> this time so within your project folder
40.16 -> we're going to create
41.28 -> two packages i do have a separate video
44.079 -> on packages
45.039 -> somewhere within this playlist so let's
47.12 -> call this first package
48.48 -> package one and all a package is is a
51.28 -> collection of classes
52.96 -> whereas a class is a collection of code
55.84 -> so
56.16 -> we'll call this package one and click
57.92 -> finish we'll create a second package
60.079 -> called
60.48 -> package2 file new package and this will
63.84 -> be
64.879 -> package 2 and click finish
68 -> we're going to create two classes within
70.159 -> each package
71.28 -> so let's click on package 1 then go to
73.76 -> file
74.32 -> new class we will call this class a and
77.52 -> check public static void main
80.88 -> let's create a second class called b
83.2 -> file new class
84.799 -> and this is also going to be within
86.32 -> package one name
88.24 -> is b then within package two we're going
91.6 -> to create a class called
93.04 -> c file new class c
96.96 -> and lastly file new class
100.079 -> make sure that this is within package
101.759 -> two and this is going to be
103.36 -> a sub this is going to be a subclass
106.479 -> of the a class so a sub
110.32 -> extends a in order for us to
113.759 -> inherit everything from the class a from
116.799 -> package one we'll need to include an
118.96 -> import
119.84 -> so import package1.a but i'm just going
122.96 -> to change this to an asterisk to import
124.96 -> everything
125.759 -> so we'll include some imports within
127.84 -> each of these classes
129.039 -> so let's begin with class a we're going
131.68 -> to import everything
133.04 -> from package 2 class b
136.08 -> import everything from package 2 class c
139.28 -> import everything from package one this
141.92 -> time
142.879 -> so with our a sub class we can now
145.92 -> inherit everything from the a class
148.08 -> within package one
149.599 -> so let's begin with axis modifiers and
152 -> we're first going to discuss
153.519 -> the default access modifier so i'm going
156.239 -> to create a string variable within
158.8 -> my c-class let's say that this is a
161.599 -> string variable called
163.519 -> default message
166.56 -> and this will display or hold this is
170.239 -> the default so currently i'm using
173.76 -> no access modifier and i would like to
176.319 -> see
177.12 -> who or which classes can see this
180 -> default message that has
181.599 -> no access modifier the default access
184.159 -> modifier let's head back to
185.68 -> our a class within package one and let's
188.48 -> create an instance of our c class
190.72 -> well i suppose we also could have made
192.72 -> this a static variable but
194.64 -> hey too late so let's create an instance
197.28 -> of the c class
198.239 -> c c equals new c
201.92 -> and i'm going to attempt to print the
205.12 -> default message variable
206.64 -> of the c class so c dot
210.4 -> default message and i'm just going to
212.799 -> copy this
213.84 -> paste it all right but we're going to
215.599 -> have a problem though and this states
217.36 -> that the field
218.159 -> c dot default message is not visible
221.28 -> that's because anything using the
223.04 -> default axis modifier
224.959 -> is only visible to anything within the
227.28 -> same package
228.319 -> so anything within package 2 is going to
230.72 -> be able to access this default message
233.12 -> variable
233.76 -> but anything within a different package
235.68 -> such as our a class
237.28 -> and b class cannot so let's attempt to
239.84 -> do this again but within our a
241.76 -> subclass so i'm going to copy
244.879 -> everything i have here including the
246.4 -> main method and just paste it within
248.799 -> our a subclass so we now actually
252.959 -> have access to our default message
255.439 -> variable within the c class
257.28 -> because these are both within the same
259.519 -> package
260.479 -> all right well for now i'm going to move
262.72 -> this main method
263.759 -> back to our a class within package one
266.8 -> so let's create another variable and
268.639 -> this is going to be a
270.16 -> public variable so let's use the public
273.759 -> modifier public this is going to be a
276 -> string and let's call this
278 -> public message and this will
281.12 -> hold a string of this
284.72 -> is public all right
287.919 -> now i'm going to attempt to access this
290.8 -> variable
291.52 -> public message from the a class which is
294.32 -> in
294.8 -> package one so c dot
298.32 -> and you can see it right here public
300.4 -> message and this will display
302.72 -> this is public that's because anything
305.199 -> that uses
306.08 -> the public keyword is visible to
309.199 -> any package within the project folder so
312 -> even though this is in package two
313.84 -> it's still visible to anything within
315.68 -> package one and a lot of other classes
317.84 -> use this
318.56 -> such as class c this is public a sub is
321.759 -> public
322.8 -> b is public and a is public now check
325.759 -> this out we have created an instance of
327.919 -> our c class within class a but what
330.8 -> would happen
331.44 -> if we removed the public axis modifier
334.4 -> from the class definition of c
336.72 -> well now currently c is using the
339.52 -> default
340.4 -> access modifier so our class c
343.6 -> is only visible to classes within the
346.56 -> same package
347.68 -> as class c if we go back to class a
351.039 -> while now class a can no longer create
353.6 -> an instance of class c
355.28 -> because the visibility of c has changed
358.16 -> class a
358.96 -> is within package 1 and we can no longer
361.36 -> access class c
362.639 -> because class c is only available to
365.28 -> classes within the same
366.8 -> package so let's change the visibility
369.68 -> of c
370.24 -> back to public so that's all what the
372.319 -> public access modifier is
374.24 -> anything that is public is available or
376.479 -> visible to other packages
378.479 -> our next access modifier is the
380.88 -> protected
382.24 -> access modifier so let's create a
384.96 -> protected variable
386.08 -> let's call this string protected message
390.56 -> and let's change the string to this is
393.759 -> protected
395.039 -> here's how the protected axis modifier
397.199 -> works so
398.24 -> something that is protected is
400.479 -> accessible
401.52 -> to a different class in a different
403.759 -> package
404.56 -> as long as that class is a subclass
408.479 -> of whatever class contains this
410.72 -> protected member
411.919 -> so for this example let's copy this line
414.479 -> of our protected message
416.319 -> and paste it within the a class we're
419.28 -> going to take our main method
421.28 -> cut it and then copy this within our a
424.479 -> subclass let's
427.599 -> remove this instance of our c class and
430.4 -> we're going to create an instance of our
432.08 -> a
432.4 -> subclass so a sub let's call this a sub
435.919 -> lower case equals new a sub
441.12 -> so a sub extends a even though a
444.56 -> is in a different package we have access
447.599 -> to this protected variable because
450.16 -> something that is protected
451.84 -> is accessible to a different class in a
454.72 -> different package
455.68 -> as long as that class is a subclass
458.72 -> of whatever class contains that
460.88 -> protected member
462.16 -> so we have an instance of our a subclass
464.96 -> and we should be able to
466.319 -> access this protected message variable
469.759 -> so system.out.printline a
472.96 -> sub dot and here is our protected
477.199 -> message
478.16 -> and it just states this is protected and
480.96 -> our last access modifier
482.72 -> is private so let's create a private
485.039 -> variable
486.08 -> private let's copy what we have for our
488.8 -> default message
489.84 -> and change default to private so this
492.639 -> will be
493.44 -> private message and the string will be
496.16 -> this
497.039 -> is private all right now let's copy all
500.639 -> of this
501.199 -> and paste it within class beep so
504.4 -> something that is private is only
506.72 -> visible to
507.759 -> the class that it contains itself so
510.8 -> only class b has access to this private
513.76 -> message
514.719 -> so i'm going to revert everything that i
517.76 -> changed for class a so let's put this
519.68 -> main method back in
521.2 -> remove the main method from a sub and
524.08 -> let's create an instance of
525.68 -> our b class so b b
528.8 -> equals new b and i would like to
532.64 -> access this private message found within
535.68 -> class b so system.out.printline
539.12 -> b dot but it doesn't appear that we can
542.72 -> actually see that
543.92 -> so let's attempt to actually print this
546.48 -> b dot private message
548.16 -> but this states the field b private
550.24 -> message is not visible
552 -> that's because something that is private
555.04 -> is only visible to the class that it
557.36 -> contains and
558.16 -> nothing else even if it's within the
560.08 -> same package well that's the basics of
562.64 -> access modifiers we have public
564.959 -> protected the default access modifier
567.519 -> and private
568.56 -> so if you would like a copy of all this
570.08 -> code i will post all of this in the
571.839 -> comments down below
573.12 -> but yeah that's the basics of access
575.36 -> modifiers
576.16 -> in java hey you yeah i'm talking to you
580.56 -> if you learn something new then you can
582.56 -> help me help you
584.48 -> in three easy steps by smashing that
586.959 -> like button
587.839 -> drop a comment down below and subscribe
590.32 -> if you'd like to become a fellow bro
606.56 -> [Music]
612.48 -> you
Source: https://www.youtube.com/watch?v=T632kAJ_9VA