Java Essentials - Access specifiers in java
3K views
Oct 24, 2024
Java Essentials - Access specifiers in java
View Video Transcript
0:00
Access specifiers in Java. Using this access specifiers we can define the access scope or the
0:09
accessibility of different data members and member functions of a class. We are having four
0:15
different types of access specifiers in Java. So let us discuss one by one. So what are the
0:23
access specifiers? Access specifiers or modifiers are used to define the scope or
0:29
accessibility of a data member or the member methods of a class so whether this
0:36
member method will be accessible from this scope or not whether a member method
0:41
or the variable rather will be used from this particular scope or not that will
0:46
be decided by this access specifiers there are four kinds of access
0:51
specifiers available in Java the first one we're going to discuss there is
0:55
a private so from the very keyword private you can get the idea that
0:59
private members are accessible only from the class body default a default
1:06
member can be accessed only from the same package obviously from the class body
1:11
and also from the same package protected protected members can be accessed by
1:18
the child classes only next one is a public there is a fourth one the last one
1:23
for public members the members can be accessed from any other class or object
1:29
a class from the different package can also access it so that means this public for the
1:36
public we can access this public members that is the member variables and member functions
1:41
from any class or objects a are also a class from the different package can also access
1:49
these members if the member is under the public specifiers so let me go for some practical
1:56
demonstration to give you a better idea on this access specifiers in Java in this demonstration we are discussing access
2:05
specifier just consider this one under the package access specified we having two classes two Java classes one is access specified or Java and the one is access
2:17
specifier same package or Java so two Java classes under the same package only
2:22
to show that that how the two classes can access their members if they are existing
2:28
in the same package so at first we're discussing access specifers same package
2:32
java here we're having only one class C having having having having
2:36
our public INTP that is a public member can be accessible from any class INTQ here we
2:43
haven't mentioned anything private public or protected so it is a default scope so default
2:49
variable can be accessible from any same package from any other class within the same package
2:55
so that means this queue will be accessible from access specifier Java class because it is
3:01
under the same package access specifier we'll be testing that one private INT R is
3:06
it is non-accessible from any other class because it is the private member
3:11
We're having the constructor passing three parameters to initialize three instance variables
3:15
and also we're having the display method which will display the current content of P, Q and
3:20
R instance variables. So that is our access specifers same package.java. Now we are going for
3:27
the another Java class that is our access specified. Here we have defined this public INTX. Public
3:34
members are accessible by any class and also inheritable private I and T Y private variables can be accessed only within this
3:42
class and they are not inheritable they are not getting inherited to the
3:46
derived class or subclass protected I NT Z protected member can be accessed in the
3:52
derived class also this is my this my constructor a passing three parameters
3:57
to initialize three instance variables we're having the display method which it display the current content of X Y Z instance variables now we are having
4:06
this class B which is inheriting extending the class A so what will happen public X will be
4:13
inherited here private I nt y will not be inherited protector Z will be
4:18
inrated and public display will be inherited here so now what we are doing here we are having the respective constructor passing three parameters so from constructor of
4:31
B set member values of class A so we are calling the best class constructor the
4:36
best class constructor will be used can be called using the super keyword so
4:41
super A comma B comma C so what will happen this A comma B comma C will be assigned to this
4:46
I n T A I nt B I nt C to initialize the instance variable of the class A we're having the display method
4:53
here which will display the value of X and Z why the value of X and Z only
4:58
because X was under the public scope and Z was under the protected scope so
5:03
they were only got inherited private member INT Y has not got inherited in class B
5:09
so from class B Y will be unaccessible so X is public member of class A and Z
5:15
is a protected member of class A so that's why they got to inherited and they can be accessed from the display method we're having this
5:23
access specifier there is a five last class here within the main function
5:27
we have due this a line we're printing there is a data of class B so A is
5:32
the name of the class the object name is class B is equal to new B 5 10 and 15 so
5:39
whenever you are calling this class B or display let us see what is happening
5:43
in case of class B or display we are finding that X and Z values are getting
5:47
displayed why that means when the whenever you are calling this class b dot display
5:52
actually this particular display method is getting called this particular display method has got called this particular display method and that's why we
6:01
could get only this x and z values are getting printed here because this
6:06
a class a class object has got instantiated with the b class constructor so
6:13
that's why class b dot display is actually calling the b class display method next one is that we are defining another class a object
6:22
that is a class a which is getting instantiated calling the class a constructor
6:27
so class a display is actually displaying the class a display method because we are getting both x y and z values are getting printed so that means this particular display method has got called and that why the x y and z
6:41
values have got printed accordingly now we are dealing with the class c c class i told you that it
6:47
it was defined within the access specifiers same package java which is another class within the same
6:54
package i told you this one earlier they are in the same package now what is happening so
7:01
class c is the object is a new see that is constructed you are calling now you are
7:05
calling this display we are calling this display what is happening you are finding
7:09
that 316 25 3 16 25 is getting printed because actually this particular
7:15
method display method is printing p2 values so using this constructor this
7:20
pqal values have got updated has got initialized so respective values have got
7:24
printed we know that public member variable p and the default scope
7:31
variable that is a Q they are accessible from other classes defined within the same
7:36
package so what will happen you can find that this class c dot p and class c
7:41
they have got initialized with this 9 and 56 so p and q they have got initialized with 9
7:47
and 56 r is not accessible from the outside of the class so you are not trying to
7:52
initialize this class c dot r so now what will happen if i type class c dot r you see
8:01
Class C is the respective you see R is not there there is no R so it is not accessible
8:08
So after initializing them if I go for the display method you can find that this 316 have got updated with 9 56 and then R is still with the value 25 so 25 was still there
8:20
So the class C dot display is printing 956 and 255 for the respective values for for the respective values for our PQ and R
8:31
So in this way we have discussed that how this access specified is playing of such a very wider role in our Java code and you have explained and demonstrated with the proper examples to clear your doubts and to give you more clarity on this subject
8:46
Thanks for watching this video
#Java (Programming Language)
#Programming
#VPN & Remote Access