Difference Between Access Specifiers and Modifiers

In Java, the access modifiers are used for restricting the scope of a class and its data members, member function, and constructor. Access modifiers play an important in designing Java programs and Java applications.

Java has the following access modifiers:

  1. private
  2. protected
  3. public
  4. default

Default Access Modifiers

When we define class, data members, and member function without specifying any access modifier, it has a default access modifier by default. The class, data members, and member functions that we defined without using access modifiers can be accessed only within the same package. Outside of the current package, we cannot use the classes and methods.

Let's implement the code for default access modifiers to understand its behavior.

DefaultModifiersExample1.java

DefaultModifiersExample2.java

Output:

When you compile the DefaultModifiersExample2 class, it throws a compile-time error.

Public Access Modifiers

It is another access modifier which we use in Java. We use the public access modifier by using the public keyword. It is one of the access modifiers which have the widest scope. We can access the methods and variables of a class from anywhere in the program, which is declared as public. In simple words, no restriction is allowed on the scope of public data members.

Let's implement the code for public access modifiers to understand its behavior.

PublicModifiersExample1.java

Output:

Access Specifiers vs Modifiers

Protected Access Modifiers

The protected is another access modifier which we use in Java by adding the protected keyword to the data members and member functions of a class. We can access the protected data members and member functions of a class within the same package or the subclasses in different packages.

Let's implement the code for protected access modifiers to understand its behavior.

ProtectedModifierExample1.java

Output:

Access Specifiers vs Modifiers

Private Access Modifiers

The private is another access modifier which we use in Java by adding the private keyword to the data members and member functions of a class. The scope of the private data member and member function is limited and can access them only within the same class in which they declare.

If we try to access them from a different package or from a different class within the same package, it will throw a compiler time error.

Let's implement the code for private access modifiers to understand its behavior.

PrivateModifierExample1.java

Output:

Access Specifiers vs Modifiers

If we uncomment the last statement in which we access the private variable passOut, it throws a compile-time error.

Access Specifiers vs Modifiers

Difference Between Access Specifiers and Access Modifiers

The meaning of both the access specifiers and the access modifiers is the same. There are no differences between the specifiers and modifiers, and the use of both is the same. The access modifier is an official term and the new term that we use instead of modifier is specifier.

So, default, public, protected, and private access modifiers can also be referred to as default, public, protected, and private access specifiers.






Latest Courses