protected
同包,同类,同父子类
错误访问
public class TestProtected {
public static void main(String[] args) throws Exception {
UserXX xx = new UserXX();
// 方法的提供者:UserXX.super.Object
// 方法的调用者:TestProtected
// UserXX,TestProtected 不在一个类,不同包,不父子类
// ==> TestProtected.super.Object != UserXX.super.Object
// 你爸是你爸,我爸是我爸,虽然我们都叫 爸爸
xx.clone(); //Error
}
}
class UserXX {}
正确访问
public class TestProtected {
public static void main(String[] args) throws Exception {
UserXX xx = new UserXX();
// 方法的提供者:UserXX
// 方法的调用者:TestProtected
xx.clone();
}
}
class UserXX {
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}