' Access Protection Modifiers in Java

Access Protection Modifiers in Java

or Visibility and Access Rights in Java

This table (matrix) shows the accessible from each class elements.

See a matrix of all modifiers in Java and the elements to which they can be applied.

Access rights for the different elements
class \ have access to private
elements
default
elements
(no modifier)
protected
elements
public
elements
own class (Base) yes yes yes yes
subclass - same package (SubA) no yes yes yes
class - same package (AnotherA) no yes yes yes
subclass - another package (SubB) no no yes/no * yes
class - another package (AnotherB) no no no yes

_______________
* Class SubB has access only to the inherited from Base protected elements, i.e. its own elements, but the protected data of other Base instances is not accessible from SubB.

Code Example

All lines with not accessible fields are commented.


package packageA;

public class Base {
    public String publicStr = "publicString";
    protected String protectedStr = "protectedString";
    String defaultStr = "defaultString";
    private String privateStr = "privateString";

    public void print() {
        System.out.println("packageA.Base has access to");
        System.out.println("    " + publicStr);
        System.out.println("    " + protectedStr);
        System.out.println("    " + defaultStr);
        System.out.println("    " + privateStr);

        Base b = new Base(); // -- other Base instance
        System.out.println("    b." + b.publicStr);
        System.out.println("    b." + b.protectedStr);
        System.out.println("    b." + b.defaultStr);
        System.out.println("    b." + b.privateStr);
    }
}

--------------------------------------------------------------------------------

package packageA;

public class SubA extends Base {
    public void print() {
        System.out.println("packageA.SubA has access to");
        System.out.println("    " + publicStr + " (inherited from Base)");
        System.out.println("    " + protectedStr + " (inherited from Base)");
        System.out.println("    " + defaultStr + " (inherited from Base)");
        // -- not accessible - private elements are even not inherited
        // System.out.println(privateStr);

        Base b = new Base(); // -- other Base instance
        System.out.println("    b." + b.publicStr);
        System.out.println("    b." + b.protectedStr);
        System.out.println("    b." + b.defaultStr);
        // -- not accessible
        // System.out.println(b.privateStr);
    }
}

--------------------------------------------------------------------------------

package packageA;

public class AnotherA {
    public void print() {
        System.out.println("packageA.AnotherA has access to");
        Base b = new Base();
        System.out.println("    b." + b.publicStr);
        System.out.println("    b." + b.protectedStr);
        System.out.println("    b." + b.defaultStr);
        // System.out.println(b.privateStr);
    }
}

--------------------------------------------------------------------------------

package packageB;
import packageA.Base;

public class SubB extends Base {
    public void print() {
        System.out.println("packageB.SubB has access to");
        System.out.println("    " + publicStr + " (inherited from Base)");
        // -- protectedStr is inherited element -> accessible
        System.out.println("    " + protectedStr + " (inherited from Base)");
        // -- not accessible
        // System.out.println(defaultStr);
        // System.out.println(privateStr);

        Base b = new Base(); // -- other Base instance
        System.out.println("    b." + b.publicStr);
        // -- protected element, which belongs to other object -> not accessible
        // System.out.println(b.protectedStr);

        // -- not accessible
        // System.out.println(b.defaultStr);
        // System.out.println(b.privateStr);
    }
}

--------------------------------------------------------------------------------

package packageB;
import packageA.Base;

public class AnotherB {
    public void print() {
        System.out.println("packageB.AnotherB has access to");
        Base b = new Base();
        System.out.println("    b." + b.publicStr);
        // -- not accessible
        // System.out.println(b.protectedStr);
        // System.out.println(b.defaultStr);
        // System.out.println(b.privateStr);
    }
}

--------------------------------------------------------------------------------

import packageA.*;
import packageB.*;

// -- testing class
public class TestProtection {
    public static void main(String[] args) {
        // -- all classes are public, so class TestProtection
        // -- has access to all of them
        new Base().print();
        new SubA().print();
        new AnotherA().print();
        new SubB().print();
        new AnotherB().print();
    }
}



Summary

Further reading