Access modifiers are the keywords public, private, and protected and is used to set the accessibility for classes, methods, constructors and attributes. A default access modifier is automatically specified if any access modifier is not specified. For classes a public or default access modifier is only used. If a class is specified with keyword public, it can be accessed from anywhere and by any other class.
The following program shows two Java classes AccessSpecifiers and anotherClass, in two different Java class files. AccessSpecifiers class get accessed from anotherClass as AccessSpecifiers is a public class.
package com.company;
public class AccessSpecifiers {
public static int x;
public static void printNumber() {
System.out.println("The integer number is "+x);
}
}
package com.company;
public class anotherClass {
public static void main(String[] args) {
AccessSpecifiers obj = new AccessSpecifiers();
obj.x = 3;
AccessSpecifiers.printNumber();
}
}
Output:
The integer number is 3
Constructors, methods attributes and data members can be public, private, protected or be default. Default modifier is executed when there is no access modifier. It is accessible only in the same package. The following program shows default access modifier. Here AccessSpecifiers class has no modifier so default is accessed.
package com.company;
class AccessSpecifiers {
int x;
float y;
String c;
public static void main(String[] args) {
AccessSpecifiers obj = new AccessSpecifiers();
obj.x = 3;
obj.y = 4.3f;
obj.c = "Any character";
System.out.println("The integer number is "+obj.x);
System.out.println("The float number is "+obj.y);
System.out.println("The string character is "+obj.c);
}
}
Output:
The integer number is 3
The float number is 4.3
The string character is Any character
When a private access modifier is used then the methods or attributes can only be accessed inside the declared class. The following program shows the private modifiers to the data members. It will be accessed only within the the same class.
package com.company;
class AccessSpecifiers {
private int x;
private float y;
private String c;
public static void main(String[] args) {
AccessSpecifiers obj = new AccessSpecifiers();
obj.x = 3;
obj.y = 4.3f;
obj.c = "Any character";
System.out.println("The integer number is "+obj.x);
System.out.println("The float number is "+obj.y);
System.out.println("The string character is "+obj.c);
}
}
Output:
The integer number is 3
The float number is 4.3
The string character is Any character
Protected Access modifier is used for attributes or methods and can be accessed by subclasses during inheritance in the same package. The following program shows use of protected modifier in data members inside a class. The data variables can only be accessed by inherited classes of the same package.
package com.company;
class AccessSpecifiers {
protected int x;
protected float y;
protected String c;
}
class Others extends AccessSpecifiers {
public static void main(String[] args) {
AccessSpecifiers obj = new AccessSpecifiers();
obj.x = 3;
obj.y = 4.3f;
obj.c = "Any character";
System.out.println("The integer number is "+obj.x);
System.out.println("The float number is "+obj.y);
System.out.println("The string character is "+obj.c);
}
}
Output:
The integer number is 3
The float number is 4.3
The string character is Any character