Different Access Modifiers in Java
Java is an object-oriented programming language that allows you to define classes and objects with different levels of accessibility. Access modifiers are used to control the access of the methods, variables, and constructors defined within a class. There are four different access modifiers in Java:
- Public
- Private
- Protected
- Default (also known as package-private)
Public Access Modifier
The most accommodating access modifier in Java is called public. A method, variable, or function Object() can be accessible from any place in the programme when it is defined as public. This implies that any other class in the software can access a class's public members.
Program
PublicAccessModifier.java
public class PublicAccessModifier {
public String name;
public PublicAccessModifier(String name) {
this.name = name;
}
public void printName(){
System.out.println("Name is: "+name);
}
public static void main(String[] args) {
PublicAccessModifier pam=new PublicAccessModifier("Jack");
pam.printName();
}
}
Output
Name is: Jack
Private Access Modifier
The most limiting access modifier is the private access modifier. You can only access a private class, method, or variable from within that class. Other courses in the programme are not able to see private members.
PrivateAccessModifier.java
public class PrivateAccessModifier {
private String name;
public PrivateAccessModifier(String name) {
this.name = name;
}
private void printName(){
System.out.println("Name is: "+name);
}
public static void main(String[] args) {
PrivateAccessModifier pam=new PrivateAccessModifier("Jack");
pam.printName();
}
}
Output
Name is: Jack
Program
Private.java
class Private{
private int n=40;
private void dis()
{
System.out.println("Hello java");
}
}
public class PrivateModifier{
public static void main(String args[]){
Private o1 = new Private();
System.out.println(o1.n);//Compile Time Error
o1.dis();//Compile Time Error
}
}
Output
PrivateModifier.java:13: error: n has private access in Private
System.out.println(o1.n);//Compile Time Error
PrivateModifier .java:14: error: dis() has private access in Private
o1.dis();//Compile Time Error
Protected Access Modifier
Access is granted to members of the same package and the class's subclasses using the protected access modifier. Other classes outside of the package cannot see protected members.
ProtectedAccessModifier.java
public class ProtectedAccessModifier {
protected int number;
protected String name;
protected void setNumber(int number) {
this.number = number;
}
protected void setName(String name) {
this.name = name;
}
protected int getNumber() {
return this.number;
}
protected String getName() {
return this.name;
}
public static void main(String[] args) {
ProtectedAccessModifier obj = new ProtectedAccessModifier();
obj.setNumber(123);
obj.setName("John");
System.out.println("Number: " + obj.getNumber());
System.out.println("Name: " + obj.getName());
}
}
Output
Number: 123
Name: John
Explanation
The protected access modifier in Java restricts access to a field, method, or constructor to only within classes in the same package or subclasses in other packages. This means that any class can access a protected field, method, or constructor in the same package as the class where the protected entity was declared and any subclass of the declaring class. This is in contrast to the private access modifier, which only restricts access to the declaring class.
Default Access Modifier
A different name for the default access modifier is package-private. A member is thought to have a default access if no access modifier is supplied. Access to members with default access is permitted only inside the confines of the package.
DefaultAccessModifier.java
public class DefaultAccessModifier {
int x = 10;
public static void main(String[] args) {
DefaultAccessModifier obj = new DefaultAccessModifier();
System.out.println("Value of x is: " + obj.x);
}
}
Output
Value of x is: 10
Program for different access modifiers in Java
AccessModifiers.java
class AccessModifiers
{
public int publicVar = 5;
private int privateVar = 10;
protected int protectedVar = 15;
int defaultVar = 20;
public void publicMethod() {
System.out.println("This is a public method");
}
private void privateMethod() {
System.out.println("This is a private method");
}
protected void protectedMethod() {
System.out.println("This is a protected method");
}
void defaultMethod() {
System.out.println("This is a default method");
}
public static void main(String[] args) {
AccessModifiers myObj = new AccessModifiers ();
System.out.println("Public variable value: " + myObj.publicVar);
System.out.println("Private variable value: " + myObj.privateVar); // This will cause a compile-time error
System.out.println("Protected variable value: " + myObj.protectedVar);
System.out.println("Default variable value: " + myObj.defaultVar);
myObj.publicMethod();
myObj.privateMethod(); // This will cause a compile-time error
myObj.protectedMethod();
myObj.defaultMethod();
}
}
Output
Public variable value: 5
Private variable value: 10
Protected variable value: 15
Default variable value: 20
This is a public method
This is a private method
This is a protected method
This is a default method