Updated March 16, 2023
Introduction to Access Modifiers in Java
As we all know, Java is one of the top programming languages in the world. Billions of devices are relying on it for the last two decades. Java is a fast, dependable, safe, and multi-platform language. Java runs on any device as long as that device has Java Runtime (JRE), making it multi-platform, fast, and dependable. Access modifier is the property of java, which makes it safe across the multi-platform. Java provides class-level safety (during encapsulation) to the programmer by using the access modifier property. According to the book, Class is the blueprint for building an object in java, which makes it a ‘Building block’ for the program as Java is an Object-Oriented language. An access modifier specifies how any class can access a given class and its fields, constructors, and methods within and different packages. Class, fields, constructors, and methods can have one of four different Java access modifiers.
- Private
- Public
- Protected
- Default – No keyword required.
Different Access Modifiers in Java
The following table summarizes how we can apply Java access modifiers to the program:
Modifiers | Class | Packages | Sub-Class | World |
Private | Y | N | N | N |
Public | Y | Y | Y | Y |
Protected | Y | Y | Y | N |
Default | Y | Y | N | N |
We will cover each Java access modifier in the following sections.
1. Default
When any class, data member, and variable is declared by not writing with an access modifier, then it is set to ‘default’ access modifier. The ‘default’ access modifier means the code inside any class can access the entire program within the same package.
- This access modifier works within the same package only.
- Sometimes, a ‘default’ access modifier is also referred to as a package access modifier, as it is accessible within the same package only.
- Subclasses could not access methods, data members, and variables (fields) in the superclass if these methods, data members, and variables (fields) are marked with the ‘default’ access modifier in the class unless these subclasses located in the same package as the superclass.
Example #1:
//Java program to show the default modifier.
package Test;
//Where Class eduCBA is having Default access modifier as no access modifier is specified here
class eduCBA
{
void display ()
{
System.out.println("Hello World!");
}
}
Output:
Hello World!
Example #2:
//Java program to show error while using class from different package with default modifier
package test2;
import test.*;
//This class check is having default access modifier
class Check
{
public static void main(String args[])
{
//accessing class eduCBA from package test
eduCBA obj = new eduCBA();
obj.display();
}
}
Output:
Compile-time error.
2. Protected
Syntax ‘protected’ is used by users when they want to use this access modifier.
- This access modifier is accessible only within the same package or same sub-classes in different classes (but users have to import that package where it was specified).
- Users cannot mark class and interfaces with a ‘protected’ access modifier. However, Methods and fields can be declared as protected If methods and fields are in an interface.
For Example:
//Java program to show to protected access modifier
package test;
//Class eduCBA
public class eduCBA
{
protected void display ()
{
System.out.println("Hello World!");
}
}
//Java program to show to protected modifier in same sub-classes of different packages
package test2;
import test.*;
//Class pro is subclass of eduCBA
class pro extends eduCBA
{
public static void main(String args[])
{
pro obj = new pro();
obj.display();
}
}
Output:
Hello World!
3. Public
Users can declare a class, method, constructor, and interface with a ‘public’ access modifier, which can access by any class, method, constructor, and interface within or different packages.
- This access modifier has the Boundless among all modifiers.
- When any class, method, or package is marked with a ‘public’ access modifier, it is accessible to everyone from anywhere from the program.
- There are no limitations on the scope of ‘public’ access class methods.
For Example: –
//Java program to show to public access modifier
package test;
public class eduCBA
{
public void display ()
{
System.out.println("Hello World!");
}
}
package test2;
import test.*;
class pub
{
public static void main (String args [])
{
eduCBA obj = new eduCBA ();
obj.display ();
}
}
Output:
Hello World!
4. Private
When a method or variable is marked as ‘private’ access modifiers, then code inside that same class can only access those methods and variables.
User can not declare any super-class with a ‘private’ access modifier in the program. If the user does so with any class, it makes that class not accessible to any other class in the same package, making class none of the use. However, the user can declare variables and methods within the class with a ‘private’ access modifier, so anyone cannot utilize those variables and methods.
Occasionally people got confused with ‘private’ and ‘protected’ access modifiers, but they both are different.
For Example: –
//Program to show error while using a class from different packages with private modifier.
package test;
class eduCBA
{
private void display()
{
System.out.println("Hello World!");
}
}
class Check
{
public static void main (String args[])
{
eduCBA obj = new eduCBA();
//make class check to access private method of another class eduCBA.
obj.display();
}
}
Output:
error: display() has private access in eduCBA obj.display();
Conclusion
Java access modifier gives you an extra edge over your program when you make it public. As we study above, Different types of access modifiers in JAVA and their specification.
So keep in mind every time you use one of them as a class or interface access as they don’t only provide access but also overrides them. While there is always a concern regarding the accessibility of the method in the program. For instance, if an interface is assigned the ‘default’ access modifier in the superclass, it can override access modifiers used in the method’s subclass.
Recommended Articles
This is a guide to Access Modifiers in Java. Here we discuss the different types of access modifiers in java and their specification. You can also go through our other suggested articles to learn more –