Difference Between Access Specifiers and Modifiers in Java
Java employs access modifiers to restrict a class's data members, member functions, and constructor. Access modifiers are essential when creating Java program and applications.
Access modifiers in Java include:
- default
- public
- protected
- private
Default Access Modifiers
Without providing an access modifier when defining a class, data member, or member function, the access modifier is set to default. Without using access modifiers, we can only access the class, data members, and member functions that we defined. The classes and methods are not usable outside of the current package.
Program for Default Access Modifiers in Java
Default_modifier1.java
package mypackage1;
class Default_modifier1 {
void display() {
System.out.println("Default_modifier1 class is invoked.");
}
}
Testing.java
\package m2;
import mypackage1.*;
public class Testing {
public static void main(String args[])
{
Default_modifier1 o1 = new Default_modifier1();
o1.display();
}
}
Output
Compile time error
Public Access Modifiers
We also use this access modifier in Java. The public keyword is used to invoke the public access modifier. It is one of the access modifiers with the most flexibility. We can access a class's methods and variables from anywhere in the programme that is designated as public. In other words, no restrictions on the extent of public data members are permitted.
Program1 for public Access Modifiers in Java
public_modifier.java
class public_modifier{
public void display()
{
System.out.println("Public Access Modifier");
}
}
Test1.java
public class Test1
{
public static void main(String args[])
{
public_modifier object1 = new public_modifier();
object1.display();
}
}
Output:

Program for public Access Modifiers in Java
Dog.java
public class Dog {
public int no_of_legs;
public void display() {
System.out.println("I am an Dog.");
System.out.println("I have " + no_of_legs + " legs.");
}
}
Test2.java
public class Test2 {
public static void main( String[] args ) {
Dog d1 = new Dog();
d1.no_of_legs = 4;
d1.display();
}
}
Output:

Protected Access Modifiers
Another access modifier that we employ in Java is the protected keyword, which we apply to the data members and member methods of a class. We can access a class's protected data members and member functions inside the same package, as well as subclasses in distinct packages.
Program for protected Access Modifiers in Java
Student.java
class Student
{
protected String roll_number = "1423423";
protected String firstName = "M.";
protected String lastName = "Rohith";
protected String email = "[email protected]";
protected int age = 21;
protected int mobile_no = 9873994;
}
Test4.java
class Test4 extends Student
{
private int passOut = 2024;
public static void main(String[] args) {
Test4 new_obj = new Test4();
System.out.println("Roll Number: " + new_obj.roll_number + "\nName: "+ new_obj.firstName + "" + new_obj.lastName);
System.out.println("Email: " + new_obj.email);
System.out.println("Age: " + new_obj.age);
System.out.println("Graduation Year: " + new_obj.passOut);
System.out.println("Mobile number: "+ new_obj.mobile_no);
}
}
Output:

Program for protected Access Modifiers in Java
Dog.java
class Dog {
protected void display() {
System.out.println("I am a Dog");
}
}
BabyDog.java
class BabyDog extends Dog {
public static void main(String[] args) {
BabyDog d1 = new BabyDog();
d1.display();
}
}
Output:

Private Access Modifiers
In Java, we apply the private access modifier by appending the private keyword to a class's data members and member methods. The class in which they are declared is the only one with access to private data members and member functions.
If we try to access them from a different package or from a different class within the same package, a compiler time error will be produced.
Program for private Access Modifiers in Java
Private_example.java
class Private_example
{
private int passOut = 2024;
}
Test6.java
public class Test6 extends private_example
{
protected String roll_number = "1423423";
protected String firstName = "M.";
protected String lastName = "Rohith";
protected String email = "[email protected]";
protected int age = 21;
protected int mobile_no = 9873994;
public static void main(String[] args) {
Test6 new_obj = new Test6();
System.out.println("Roll Number: " + new_obj.roll_number + "\nName: "+ new_obj.firstName + "" + new_obj.lastName);
System.out.println("Email: " + new_obj.email);
System.out.println("Age: " + new_obj.age);
System.out.println("Graduation Year: " + new_obj.passOut);
System.out.println("Mobile number: "+ new_obj.mobile_no);
}
}
Output
Compile time error
Here, passOut is declared as a private in private_example. Hence, it throws a compile time error while accessing it from another class.
Program for private Access Modifiers in Java
Student.java
class Student {
private String s_name;
}
Test5.java
public class Test5 {
public static void main(String[] main){
Student N = new Student();
N.s_name = "Rohith";
}
}
Output:

We are attempting to access the Student class's private variable from the Test5 class, which causes an error to be generated.
Overcoming the private compile time error
We can utilize the getters and setters’ function to access those private variables without causing a compile-time error.
Program for private Access Modifiers in Java
Student.java
class Student {
private String s_name;
public String getName() {
return this.s_name;
}
public void setName(String s_name) {
this.s_name= s_name;
}
}
Test8.java
public class Test8 {
public static void main(String[] main){
Student s1 = new Student();
s1.setName("Rohith");
System.out.println(s1.getName());
}
}
Output:

What is the distinction between Access Specifiers and Access Modifiers?
In Java, there is no distinction between an access specifier and an access modifier. Both have the same meaning. Access specifier has been replaced with a new, official term called access modifier. For setting access levels for classes, variables, methods, and constructors, Java has four access modifiers.