[JAVA] 정보은닉

Sangho Kim·2022년 9월 23일
0

JAVA

목록 보기
8/17

정보은닉이란?


  • 클래스 내부에서 사용할 변수나 메서드를 private로 선언해 외부에서 클래스 내부의 정보에 접근하지 못하도록 하는 기능을 객체지향에선 정보은닉이라고 말한다.

예제 코드

public class Practice {
    private int a;  //은닉화
    public void setA(int n){
        a = n;
    }
    public int getA(){
        return a;
    }
}

public class Practice2 {
    public static void main(String[] args) {
        Practice v = new Practice();
        //System.out.println(v.a); //호출불가

        v.setA(100);
        System.out.println(v.getA());
    }
}
  • 위와같이 getA 메소드는 public으로 지정되어 외부 클래스의 내용 참조가 가능하지만
    a는 private으로 정보은닉이 되어 Practice2 클래스에서는 호출이 되지 않는다.
profile
Immediately, certainly, until it becomes.

0개의 댓글