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 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 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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值