What are java access specifiers?


Java has total four access specifier private, protected, public and default(package) access specifiers. When used, the Java access specifiers public, protected, and private are placed in front of each definition for each member in your class, whether it’s a field or a method.

Package access

The default access has no keyword, but it is commonly referred to as package access (and sometimes “friendly”). It means that all the other classes in the current package have access to that member, but to all the classes outside of this package, the member appears to be private.

Since a compilation unit(a file)can belong only to a single package, all the classes within a single compilation unit are automatically available each other via package access.

Package access allows you to group related classes together in a package so that they can easily interact with each other.

For classes, and interface declarations, the default is package private. This falls between protected and private, allowing only classes in the same package access. (protected is like this, but also allowing access to subclasses outside of the package.

class MyClass   // package private
{
   int field;    // package private field

   void calc() {  // package private method

   }
}

For interface members (fields and methods), the default access is public. But note that the interface declaration itself defaults to package private.

interface MyInterface  // package private
{
   int field1;         // static final public
   void method1();     // public abstract
}

If we then have the declaration

public interface MyInterface2 extends MyInterface
{

}

Classes using MyInterface2 can then see field1 and method1 from the super interface, because they are public, even though they cannot see the declaration of MyInterface itself.

Public access specifier(interface access)

When you use the public keyword, it means that the member declaration that immediately follows public is available to everyone, in particular to the client programmer who uses the library. Suppose you define a package dessert containing the following compilation unit

public class Program {
    //available to everyone
    public static final String C1 = "C!";

    public int i, j;//available to everyone
    
    //Constructor available to everyone
    public Program(){
        
    }
    
    //Method available to everyone
    public void sayHello(){
        System.out.println("Hello");
    }
}

Private access specifiers(no one can touch)

The private keyword means that no one can access that member except the class that contains that member, inside methods of that class. Other classes in the same package cannot access private members, so it’s as if you’re even insulating the class against yourself.

public class Program {
    //no outside access
    private static final String C1 = "C!";

    private int i, j;//no outside access
    
    //no outside access
    private Program(){
        i = 10; //can access inside
        j = 10;
        sayHello();
    }
    
    //no outside access
    private void sayHello(){
        System.out.println("Hello");
    }
}

Protected access specifiers(inheritance access)

Simply put, protected means that the method (for example) will be accessible only within the subclasses and the package in which it\’s defined.

Modifier

Class

Package

Subclass

World

public

Y

Y

Y

Y

protected

Y

Y

Y

N

no modifier

Y

Y

N

N

private

Y

N

N

N