.png)
private : 클래스 내에서만 접근 가능
default : 해당 패키지 안에서만 접근 가능
protected : 해당 패키지 + 패키지는 다르지만 상속 관계의 클래스는 접근 가능
public : 서로 다른 패키지에서도 접근 가능
접근범위
private < default < protected < public 순으로 보다 많은 접근을 허용한다.
필드 접근자를 private 선언으로 무차별적인 접근 제한
set,get 메소드를 정의해서 필드에 접근이 가능하도록 한다.
public class Rectangle {
private int width;
private int height;
private int area;
public Rectangle() {
}
public Rectangle(int width, int height) {
super();
this.width = width;
this.height = height;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getArea() {
return area;
}
public void setArea(int area) {
this.area = area;
}
}