Access Specifiers

Java Access Specifiers (also known as Visibility Specifiers) regulate access to classes, fields, and methods in Java.These Specifiers determine whether a field or method in a class can be used or invoked by another method in another class or sub-class. Access Specifiers can be used to restrict access.

Let’s take an example to understand Java Access Specifiers clearly:

Nowadays, everybody is familiar with Facebook and it has privacy settings for content you post, which you can customize as per your requirements. Like you can make your Post-Public (Visible to Everyone), Private (Visible to You only), Protected (Visible to Friends of Friends) and Default (Visible to Friends only).

Access specifiers are an integral part of object-oriented programming. Also, they (or access specifiers) are keywords in OOPS that set the accessibility of classes, methods, and other members and these modifiers are a specific part of programming language syntax used to facilitate the encapsulation of components.

Java provides a number of access modifiers to help you set the level of access you want for classes as well as the fields, methods, and constructors in your classes.

The four access levels are:

  • Visible to the package the default. No modifiers are needed.
  • Visible to the class only (private).
  • Visible to the world (public).
  • Visible to the package and all subclasses (protected).

The following table summarises that what access specifiers may be assigned to different elements

Access Specifiers
  Different Class Same Package Different Package
Same Class
Child Class
Child Class
Non- Child
Class Class
Public Yes Yes Yes Yes Yes
Protected Yes Yes Yes Yes No
Default Yes Yes Yes No No
Private Yes No No No No


Default Access Modifier – No keyword:

  • A default member may be accessed only if the class accessing the member belongs to the same package, whereas a protected member can be accessed (through inheritance) by a subclass even if the subclass is in a different package.
  • A variable or method reported with no access-control modifier can be obtained to another group in exactly the same program.
  • The areas within an interface are implicitly public static closing and the strategy within an interface are automatically people.
  • Default and protected behavior differ only when we talk about the subclass.If the protected Keyword is used to define a member, any subclass of the class declaring the member can access it through inheritance.Whereas default access doesn’t extend any special consideration to subclasses, so when you think of default access, think package restriction.


Variables and methods can be declared without any modifiers, as in the following examples:


String version = “1.5.1”;

boolean processOrder() {

return true;


Private Access Modifier – private:

  • Members marked private can’t be accessed by a code in any class other than the class in which the private member was declared.
  • A private member is invisible to any code outside the member’s own class.When a member is declared private, a subclass can’t inherit it.
  • Can a private method be overridden by a subclass?That’s an interesting question, but the answer is technically no.Since the subclass cannot inherit a private method, it, therefore, cannot override the method–overriding depends on inheritance.
  • Techniques, Variants, and Constructors which can be declared private can only be obtained within the stated type itself.
  • Variants which are declared private can be accessed outside the group if community getter methods can be found in the group.


The following class uses private access control:

public class Logger{
private String format;
public String getFormat(){
return this.format;
public void setFormat(String format){

Here, the structure variant of the Logger class is personal, so there aren’t any means for additional categories to recover or set its value immediately.

Therefore to get this variable available to the external world, we described two public methods: getFormat(), which yields the value of the structure, and setFormat(String), which establishes its worth.

Public Access Modifier – public:

  • When a method or variable member is declared public, it means all other classes, regardless of the package they belong to, can access the member(assuming the class itself is visible).
  • For a subclass, if a member of its superclass is declared public, the subclass inherits that member regardless of whether both classes are in the same package.

Protected Access Modifier – protected:

  • The protected fields or methods cannot be used for classes and Interfaces. It also cannot be used for fields and methods within an interface.
  • Fields, methods, and constructors declared protected in a superclass can be accessed only by subclasses in other packages.
  • Classes in the same package can also access protected fields, methods and constructors as well, even if they are not a subclass of the protected member’s class.
  • Remember that any time talks about a subclass having an access to a superclass member, we could be talking about the subclass inheriting the member, not simple accessing the member through a reference to an instance of the superclass.


package other;                                      //Different Package

import certification.Parent;

class Child extends Parent {

public void testIt() {

System.out.println("x is " + x);                   //No problem;Child inherits x


The Following parent class uses protected access control, to allow its child class override openSpeaker() method:

Class AudioPlayer{
protected boolean openSpeaker(Speaker sp){
//implementation details
Class StreamingAudioPlayer{
Boolean openSpeaker(Speaker sp){
//implementation details

Here, if all of us determine openSpeaker() procedure as personal, then it wouldn’t normally be reachable from another group additional than AudioPlayer. If we all determine it as people, then it might become accessible to each of the external planet. But our in tension would be to uncover this system to its subclass simply, that’s why we utilized shielded modifier.

Most Frequently asked questions on Access Specifiers

No, because private members of superclass are not visible to subclasses
If we will make the constructor private then only members of your class will be able to create its object
If two Threads(suppose T1 and T2) are accessing the same object and updating a volatile variable then it means T1 and T2 can make their own local cache of the Object except for the volatile variable. So the volatile variable will have only one main copy which will be updated by different threads and updating by one thread to the volatile variable will immediately reflect the other Thread.
No, we cannot reduce overridden method accessibility.