In a java, as the name suggests the access modifier is used to limit the scope of the constructor, class, method, and fields to access from outside of the class or outside of the package.
There are four types of access modifier in java and they are as follows
- public
- private
- default
- protected
public | protected | default | private | |
---|---|---|---|---|
Same class | YES | YES | YES | YES |
Same package sub class | YES | YES | YES | NO |
Same package non-sub class | YES | YES | YES | NO |
Different package sub class | YES | YES | NO | NO |
Different package non-sub class | YES | NO | NO | NO |
Why do we need the access modifier?
Sometimes we need to restrict the constructors, methods, or fields to access by other classes so that they can't access and modify them.
It's always a good practice to use the correct access modifier for constructors, methods, or fields.
private access modifier
The private access modifier in java is used with the private keyword.
The private access modifier in java is only accessible from within a class. If you will try to access a private access modifier outside of the class, then you will get an error which we will examine in the following of this tutorial.
In the above java program, I have created two java classes i.e. Tutorial
and TutorailB
inside the same package i.e. LearnJavaSkills.in
.
Inside the Tutorial class, I have created a getTutorial()
method, If you look-over to getTutorial() I have used a private access modifier.
package LearnJavaSkills.in;
public class Tutorial {
private void getTutorial() {
System.out.println("LearnJavaSkills.in");
}
}
public class TutorailB {
public static void main(String[] args) {
Tutorial tutorial = new Tutorial();
tutorial.getTutorial();
}
}
If we'll execute this program, then we will get this error because, in java, the private access modifier will restrict to the outside of the class to access it.
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method getTutorial() from the type Tutorial is not visible(alert-error)
In the above java program, we are trying to access a private method in the outside of the class, Hence we've got this error
public access modifier
Unlike the private access modifier, The public access modifier is accessible from anywhere of the project.
The public access modifier is used when we need that method, fields, or constructors to be accessible from anywhere. It is always a best practice to avoid using public access modifier unless and until you need to use it.
Let's take the same example which we took for a private access modifier, but this time we will modify the access modifier of getTutorial method to the public instead of private.
package LearnJavaSkills.in;
public class Tutorial {
public void getTutorial() {
System.out.println("LearnJavaSkills.in");
}
public class TutorailB {
public static void main(String[] args) {
Tutorial tutorial = new Tutorial();
tutorial.getTutorial();
}
}
If we execute this program, then we will get the following output
LearnJavaSkills.in(code-box)
default access modifier
In java, The default access modifier is the default access modifier. If we don't grant access modifier to the constructors, method, fields, or the class then, the java assigns them a default access modifier to them.
The default access modifier is only accessible within the class, with in the same package to the subclass, or with in the same package to the non-sub class.
Let's create a simple java program using default keywords, but we will try to access the default method outside of the package and we will look-over which error we will face.
package LearnJavaSkills.in;
public class Tutorial {
void getTutorial() {
System.out.println("LearnJavaSkills.in");
}
}
package LearnJavaSkills.in.TutorialB;
import LearnJavaSkills.in.Tutorial;
class TutorailB {
public static void main(String[] args) {
Tutorial tutorial = new Tutorial();
tutorial.getTutorial();
}
}
error
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method getTutorial() from the type Tutorial is not visible(alert-error)
In the above java program, we have created two packages i.e. LearnJavaSkills.in and LearnJavaSkills.in.TutorialB
, And we are trying to access getTutorial()
method which has a default access modifier, and hence we've got this error.
protected
The protected access modifier is used within the same class, within the same package of the subclass, within the same package but non-subclass, and different package subclass
Let's create a simple java program using a protected access modifier
package LearnJavaSkills.in;
public class Tutorial {
protected void getTutorial() {
System.out.println("LearnJavaSkills.in");
}
}
package LearnJavaSkills.in.TutorialB;
import LearnJavaSkills.in.Tutorial;
public class TutorailB extends Tutorial {
public static void main(String[] args) {
TutorailB tutorial = new TutorailB();
tutorial.getTutorial();
}
}
In the above java program, we have designed two different packages, such as LearnJavaSkills.in and LearnJavaSkills.in.TutorialB. And we've designed two classes in each package, Tutorial, and TutorailB respectively.
We've used the protected access modifier, in the getTutorial()
method of the Tutorial
class, which resides in the LearnJavaSkills.in.Tutorial
package. And here, we are calling getTutorial() method in the TutorailB class.
Here, is the output of the above java program
LearnJavaSkills.in(code-box)
If you take a close look in the above access modifier picture, you will notice this java program is the example of different pacakge but subclass and also used inheritance java property.
If you enjoy this tutorial, then please subscribe to this blog so that you don't miss in future my published articles. The subscription link is in the header section of this article and as well as in the sidebar of this article
Conclusion
In this access modifier in java tutorial, we've learned what is access modifier in java, what are the various types of access modifiers in java are used and how to use them. we also check the error, if we try to access the constructor, method, or field in the restricted area.
Thanks for reading and keep learning