public > protected > default > private
제어자 | 같은 클래스 | 같은 패키지 | 자손 클래스 | 전체 |
---|---|---|---|---|
public | O | O | O | O |
protected | O | O | O | |
(default) | O | O | ||
private | O |
package access1;
public class Myparent {
private int pri;
int def;
protected int pro;
public int pub;
public void printMember() {
System.out.println(pri);
System.out.println(def);
System.out.println(pro);
System.out.println(pub);
}
public static void main(String[] args) {
Myparent p = new Myparent();
System.out.println(p.pri); //OK
System.out.println(p.def); //OK
System.out.println(p.pro); //OK
System.out.println(p.pub); //OK
}
}
package access1;
public class Myparent {
private int pri;
int def;
protected int pro;
public int pub;
public void printMember() {
System.out.println(pri);
System.out.println(def);
System.out.println(pro);
System.out.println(pub);
}
}
class Mydef{
public static void main(String[] args) {
Myparent p = new Myparent();
System.out.println(p.pri); //에러
System.out.println(p.def); //OK
System.out.println(p.pro); //OK
System.out.println(p.pub); //OK
}
}
package access2;
import access1.Myparent;
public class Mypro extends Myparent {
public void printMember() {
System.out.println(pri); //에러
System.out.println(def); //에러
System.out.println(pro); //OK
System.out.println(pub); //OK
}
}
package access2;
import access1.Myparent;
class MyTest {
public static void main(String[] args) {
Myparent p = new Myparent();
System.out.println(p.pri); //에러
System.out.println(p.def); //에러
System.out.println(p.pro); //에러
System.out.println(p.pub); //OK
}
}