Access Specifiers in Java
Access specifiers in Java(also called access modifiers) are keywords utilized to indicate the visibility (availability) of classes, methods, and variables. They control the level of access to members of a class from other classes or packages.

There are four main types of access specifiers in Java:
public
private
protected
- Default (Package-Private) – No specifier used
Each access specifier in java defines how the members (fields, methods, classes, etc.) can be accessed.
1. public
Access Specifier
The public
access specifier is the least restrictive. A member declared as public
can be accessed from anywhere in the program.
- Class: A class declared as public can be gotten to from any other class.
- Method/Field: A method or field declared as public can be gotten to from anyplace.
Example of public
access specifier:
In this example, the Car class, the brand field, and the displayDetails method are all public, so they can be accessed anywhere, even from other classes.
2. private
Access Specifier
The private access specifier is the foremost prohibitive. A member declared as private can as it were be accessed inside the same class. It cannot be accessed outside the class, not indeed by subclasses or classes within the same package.
- Method/Field: A private method or field can only be accessed inside the class where it is defined.
Example of private
access specifier:
Here, the brand field and displayDetails method are private, so they can’t be accessed directly outside the Car class. We use public methods (setDetails and showDetails) to access them indirectly.
3. protected
Access Specifier
The protected access specifier permits access to a member inside the same package additionally by subclasses, indeed if they are in several packages.
- Class: A protected class can as it were be accessed inside the same package or by subclasses.
- Method/Field: A protected method or field can be accessed within the same package or by subclasses (even if they are in different packages).
Example of protected
access specifier:
In this example, the brand, year, and displayDetails members are protected in the Car class, so they can be accessed by a subclass (SportsCar) within the same package or from a subclass in another package.
4. Default (Package-Private) Access Specifier
When no access specifier is mentioned (i.e., no public, private, or protected), the member has default access (too known as package-private). This implies the member is accessible as it were inside the same package.
- Class: A class with default access can as it were be accessed inside the same package.
- Method/Field: A method or field with default access can only be accessed within the same package.
Example of Default Access Specifier:
Here, the Car class, the brand field, and the displayDetails method are default, so they can only be accessed by classes within the same vehicles package.
Summary of Access Specifiers in Java:
Access Specifier | Can Access Within Same Class | Can Access Within Same Package | Can Access In Subclass (Different Package) | Can Access From Anywhere |
---|---|---|---|---|
public | Yes | Yes | Yes | Yes |
private | Yes | No | No | No |
protected | Yes | Yes | Yes (In subclass) | No |
Default (Package-Private) | Yes | Yes | No | No |
Note:
- Utilize public once you need the class, method, or field to be accessible from any other class.
- Utilize private once you need the class members to be accessible as it were within the same class for information encapsulation.
- Utilize protected after you need members to be accessible by subclasses or other classes within the same package.
- Utilize default access (no modifier) after you need the members to be accessible as it were inside the same package.
This gives you adaptability in controlling the accessibility of your class members in Java.