java 访问权限

来源:互联网 发布:第三方数据服务商 编辑:程序博客网 时间:2024/06/05 10:03

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 elementsclass \ have access toprivate 
elementsdefault 
elements 
(no modifier)protected
elementspublic 
elementsown class (Base)yesyesyesyessubclass - same package (SubA)noyesyesyesclass - same package (AnotherA)noyesyesyessubclass - another package (SubB)nonoyes/no *yesclass - another package (AnotherB)nononoyes

* Class SubB has access only to the inherited from Base protected elements, i.e. its own elements, but the protected data of other Baseinstances 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 classpublic 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