JAVA PROGRAMMING TUTORIALS (Lessons For Beginners) EP8 – Access Modifiers

JAVA PROGRAMMING TUTORIALS (Lessons For Beginners) EP8 – Access Modifiers


Hello I’m George This is the 8th lesson of my Java Programming tutorial series In this episode I’m going to show you the access modifiers in Java So let’s get started First of all you need to know what kind of access modifiers exists in Java. We know 4 types of access modifiers public, protected, no modifier and private For demonstrating these access modifiers We are going to create 4 classes in
2 different packages So first of all let’s create the first class
which will be a.AParent So it will be the AParent class of the a package. And the second class in this package will be AChild. Now we can create the second
package which will be called b. It will have a BParent and a BChild. After we created these classes we are going to Write some code to the a parent class. We are going to add some fields there. A private field A no access modifier field And the protected field All of these fields will be booleans for the simplicity of
this video And we will have a public field And I will add the suffix a to this field so We could know that these are in the
apparent class So I am going to create a new method now
which will be called accessFields This method will basically try to print the
values of all these fields And it will print the fields name and the fields value So for example privateFieldA is privateFieldA
noModifierFieldA is noModifierFieldA protectedFieldA is protectedFieldA And of course publicFieldA is publicFieldA After a reformatting the code we are going to do the same thing in the other classes too But before that we are going to create an
interface which will have a method called accessFields. The interface will
be called FieldAccessTester. And all the methods of the interface are public methods, so we don’t even need to include the public modifier there, because we know
that all the methods of the interfaces are public so the AParent class will
implement this FieldAccessTester And it will basically tell us that we can
refer to the AParent as a FieldAccessTester Since it implements this
interface The AChild will also implement this interface and if we invent the
interface then we also need to implement the method of course so we are going to
press ALT+ENTER and click to implement methods and press ok to implement this
accessFields method and we are going to copy the code of the AParent class to the AChild class and rename the fields. Add the Child suffix there like a privateFieldAChild,
no modifierFieldAChild protectedFieldAChild and the publicFieldAChild and this AChild will extend the AParent class and if we
extend the AParent class we don’t even need to put the interface here, because the AParent
class already has that interface. Which means that we will also have it in the AChild without mentioning it again. So I’m going to copy these printings and as
you can see the private fields are For some reason red so it
means an error and I’m going to show you what is this error actually I’m going to
print all of these 8 so as you can see it’s like that and actually I can’t
print all the 8, because private fields are only accessible from the classes
where they are where declared And I can access all the other fields which has other modifiers so now we are going to first of all add some code BParent and
BParent will actually extend the AParent class And we need to implement this accessFields method of course and we are going to just copy this whole code
there from the AParent and rename all these fields to privateFieldB, noModifierFieldB, protectedFieldB and publicFieldB. We are going to copy all
these tests what we made in the aChild class Wow as you can see there are lots of
errors. First of all we can’t really access the no modifier field and the
reason for that is that no modifier fields can only be accessed from the
same package where they were declared and since this no modifier field a
was declared in the package a and not in package b it cannot be
accessed from the package b. So what about these other 4? We just need to rename them, because we want to access our own fields. So the own fields of
the B parent class. And as you can see there is no issue there You can access all the fields independently from the modifier from the same class at all the
time so let’s do something with the BChild class too. And this will be a special
class I don’t want to make it being the child class of the BParent class so
I’m going to do something else here and What I’m going to do will be creating
new instances of these previous classes So for example create a new AChild, also
create a new AParent and of course Create a new BParent instance so we
have 3 instances of 3 different classes now. and let’s just try to access
all of these fields what these 3 classes have actually. For doing that we need to add the objects name (dot) field name of course. You already know that, because I have shown you this in previous episodes of this series. If you didn’t know that you need to access it that way
you should watch these previous episodes since this is a video tutorial so you
always have a chance to watch the previous videos if you don’t remember
for something what I am doing in the last episode. And of course you already
know what are these errors for a no modifier and the private field. Private
fields are not accessible because we are not trying to access them from the same
class and no modifier fields are not accessible because we are not trying to
access them from the same package because we are in the b package now and
not in the package a but as you can see we also can not access the
protected field and the reason for that is that protected fields can only be
accessed by classes in the same package or classes extending the class where
these fields were declared. I’m going to add these comments there so when you
download this source code from the GitHub you will be able to see what are
the reasons behind these fields not being accessible. And of course public
fields can always be accessed so there is no issue with them if you just make
all of your fields public then you won’t need to care about such stuff but you
don’t really want to do that because you want to hide some of your fields from
the client code so from other people’s code who are using your software as a
library or who are just extending your software so you want to hide some of
your fields from those people and from those applications As you can see the same applies to AChild.
So I’m not going to write this down again because that won’t
make any sense actually so what we can do now is check the BParent and it has
the same thing basically. So we can check the BParent now and print all these
fields for the BParent too And as you can see we can access almost all of
these fields except the private of course but we have no issue with
accessing the no modifier field and the protected field and the public field
because we are in the b package and not in the a package. So the thing, what I told you
is actually true that’s the proof for it and the last thing what I am going to
show you in this video will be the way how you can apply these access modifiers
to methods instead of fields but before doing that we are going to test this
program so we are going to create a array of FieldAccessTesters, which will
contain a new instance of all of these FieldAccessTesters so all the classes
which we created and which are implementing the FieldAccessTester
interface and we will just loop through them and print the result of the accessFields method. We can use the for loop here, Like we did with the lists and now
you also know how to create these arrays in Java and
we can also refer to the class name using the t.getClass().getSimpleName()
and if we want to get the fully classified class name so the class
name with the full package name then we can do t.getClass().getName() So let’s run the program so you could see what is the result of all of these things what
we wrote here. And you see everything is working as I said except there is an
issue… Did you spot the issue? Did you spot it? That’s not an issue that’s fine So what is the issue what do you think comment below comment below right now,
before I show you it Did you comment it? I hope you did comment it You still have some time to do it Ok so if you have already commented or you already commented that you don’t know what the issue is I’m going to show you what is
it and the only issue we had is with this testing BParent and we are
testing the AChild there That’s not making sense the privateFieldAChild should not be
accessible from the BParent. And the reason for these messages are that I
just used incorrect names there in the System.out.println line so we are
going to fix this issue quickly and after fixing this issue we can run the
program again And as you can see it works fine So the last thing what I am going
to show you in this video will be the access modifiers
apply to methods so first of all just try to create a new method called
privateAChildMethod which we print called privateAChildMethod and it
should be just void method instead of boolean, because I’m not really
increased about any kind of return value here so called privateAChildMethod and
we are also going to call it of course and we are going to create new methods
for the other access modifiers as well but before doing that we are going to
call this private… How was it called? privateAChildMethod As you can see
the same applies to methods what applied to fields, that
private methods can only be called from the same class where they were declared
so you cannot call any kind of private methods of other classes at all. And let’s create a no access modifier method now and of course a protected method and
also create a new public method. Try to call these methods now from the BChild
class so we go to the BChild class and we write aChild.protectedAChildMethod. As you can see IntelliJ shows it with red color, because AChild classes
protectedAChildMethod can only be accessed from the AChild class or from
any class which extends the AChild class or from classes which are in the same
package where the AChild class is. Now we are going to call the AChild classes noModifierAChildMethod, which can also not be called from here, because no
access modifier methods can only be called from the classes, which are in the
same package as the class, where the method is declared. And now we can try to
access the AChild classes publicAChildMethod And as you can see IntelliJ IDEA
is not showing any error here which means that it can be accessed because
public methods can be accessed from everywhere basically. Let’s try to do the
same thing in the BParent class To see what’s the difference here and of course
we need to create these methods so we are going to create a new protectedAChildMethod and now let’s copy this code to the BParent class and try to
access the methods of AParentClass which we are going to create right now
so we are going to initialize a new AParent, instead of AChild and we are
going to rename it to be aParent and of course change all these references to it. We are going to create this new protectedAParentMethod We can use the
SHIFT + F6 for renaming a method but we are not going to do that because it will
also rename the previous method which has this name, so we don’t want to do it.
We are just going to do it manually. As you can see the protectedAParentMethod
can not be accessed Because…
It’s kinda interesting… Why can’t it be accessed?
What do you think? comment below what do you think why
can’t it be accessed From the BParent class, since the BParent class is extending the AParent class, so this is kinda weird and I want you to figure
out why is that, but I will tell it in the end of this video We are going to create
a new method which will be the no modifier method So aParent.noModifierParentMethod And as you can see that also cannot be accessed from the BParent
class, but we are extending the AParent So what is going on?
What do you think? Really…
So weird, that we can’t access all these methods When we could have accessed them
if they were fields actually So so you know the secret?
Why is that we can’t access them? Make sure to comment
below if you know it Make sure to comment below if you don’t know it then also comment below and just guess… Write some guesses what do you think why can’t
we access these two methods. Also if you didn’t subscribe to my YouTube channel yet
make sure to do so and also Click to the notification bell so you could get
notified when I upload my next episode of this series and of course click to
the like button and like other people’s comments too if you think that what they
said is relevant to this content So now I am going to show you the secret. Are you ready? I’m going to write here as a comment what is going on So protected methods can not be called only overrided by subclasses Yes that’s right, so I’m going to show you how to override these so like that you just start to write the
name of the methods and IntelliJ IDEA will offer you this option to actually
override it and the code action looks like that That you write @Override annotation
and the methods name and that’s it basically and after it’s overriden you can access them. So that’s the tick.
That’s what you need to do. And there is a little problem as you can see. The noModifierParentMethod can not be even overridden because it can only be accessed from the
a package so the same applies here what applies to the fields too about these no
modifier methods And now it’s time for a homework. So your homework after
watching this video will be doing the following: Make a C class having 3
public methods extending the AParent and add this C class to these fieldAccessTesters array. It’s pretty simple, so I hope you will be able to do it and
make sure to leave a comment with your solution of your homework so others
could see it.

4 thoughts on “JAVA PROGRAMMING TUTORIALS (Lessons For Beginners) EP8 – Access Modifiers”

  1. So if you are serious about learning Java you can leave at least 3 comments on this video, and a 4th with your feedback as a separate comment.
    So under this comment write the solution to the homework.

Leave a Reply

Your email address will not be published. Required fields are marked *