Access Specifiers restricts access to classes, interfaces, methods, and fields in Java. It sets the domain of the specific Field, Method, or Class. In simple terms, the specifiers determine whether the particular method or field can be accessed by another sub-class or Class.
Access Specifier and Access Modifier, both the terms are used simultaneously and, they mean the same thing.
Before we talk about Access Specifiers or Access Modifier type, first we need to understand what is the use of access specifiers:
In Java, Pointers does not exist, unlike C and C++. The particular reason for this circumstance is, it gives direct access to memory, which anyone can access.
As we know, the pointers hold the address of the variable, and if we can not restrict it to access the address for that variable where our sensitive data exist, then we can’t build secure software. Therefore, Java came up with a superb solution: Encapsulation. Encapsulation is one of the best features of the Java language, which you can implement using Access Specifiers.
What are the Various Access Specifiers for Java Classes?
There are four access specifiers in Java a) Private b) Default c) Protected and d) Public. Let’s understand them briefly:
Private Specifier
It has the lowest level of accessibility. The methods and classes with this access specifier can only be accessed within the same class. These methods and fields are invisible to the subclasses. Also, A subclass cannot inherit a class with a private access specifier. A programmer can encapsulate and hide the data using the Private keyword. You can not use Private Keyword for a top-level class, else it will show a compilation error.
class A { private int data=40; private void msg() { System.out.println("Hello java"); } } public class B{ public static void main(String args[]) { A obj=new A(); System.out.println(obj.data); //Compile Time Error obj.msg(); //Compile Time Error } }
Here, in this example, You are trying to access a private field of class A in class Simple. Therefore, it will show a compile-time error.
Default/No Specifier
It has a bit more accessibility in contrast to Private Specifier. You do not need to specify any keyword for this Specifier. If You don’t mention the Default keyword, JVM automatically understands, it is a Default / No Specifier.
In Software Development, individuals work in packages, which are similar to folders. If a Specifier is not mentioned, then the Class becomes a Default class, and we can only access their members only in that package. We can’t access or inherit any member or class outside this package. But you can access and inherit in that package.
package first; class A { void msg() { System.out.println("Default"); } } package second; import first.*; class B { public static void main(String args[]) { A obj = new A(); //Compile Time Error obj.msg(); //Compile Time Error } }
Class A and its method have default specifier. Therefore you cannot access them from outside their package.
Protected Specifier
You can apply a protected specifier to data members, constructors, and methods. Using this keyword for a class will throw an error stating “Modifier Protected is Not Allowed Here”. Therefore, avoid using it for Classes. A protected Specifier has more accessibility compared to the Default one.
This modifier has an advantage over the Default / No Specifier. It allows you to access the class members outside their package, but this is possible only through inheritance. It means you have to make that class the sub Class or child class of the other class, which is in another package.
To make a field Protected, you have to use the protected Keyword before the data type of the field.
package first; class A { protected void msg() { System.out.println("Protected"); } } package second; import first.*; class B { public static void main(String args[]) { A obj = new A(); obj.msg(); } }
Since the field of class A is protected, it can be accessed by the class B of a different package.
Public Specifier
In terms of accessibility Public class has the highest level. If a Class is Public, anyone can access their Members. Any class can inherit or use their members. To make a class Public, you have to use the public Keyword Before Class Keyword.
package first; class A { public void msg() { System.out.println("Protected"); } }
Conclusion
Access Specifier gives two advantages: Encapsulation and the other is security. Here Capsule is our Packages and sometimes classes too.
By Restricting we are providing more security. We should use Access Specifier as much as possible so we can Build more Secure, Reliable, and full proof software. Java classes and interfaces can use either Default or Public specifier for themselves.