본문 바로가기

java공부

[객체지향프로그래밍]07_접근 제어 지시자(access modifier) , 정보은닉(infomation hiding)

📌  접근 제어 지시자 (access modifier)


  • 클래스 외부에서 클래스의 멤버 변수, 메서드, 생성자를 사용할 수 있는지 여부를 지정하는 키워드
  • private : 같은 클래스 내부에서만 접근 가능 (외부 클래스, 상속 관계의 클래스에서도 접근 불가)
  • default (아무것도 없음) : 같은 패키지 내부에서만 접근 가능 (상속 관계라도 패키지가 다르면 접근 불가)
  • protected : 같은 패키지나 상속관계의 클래스에서 접근 가능하고 그외 외부에서는 접근 할 수 없음
  • public : 클래스의 외부 어디서나 접근 할 수 있음

 

 

 

 

 

📌 get() / set() 메서드


  • private 으로 선언된 멤버 변수 (필드)에 대해 접근, 수정할 수 있는 메서드를 public 으로 제공
  • get() 메서드만 제공 되는 경우 read-only 필드
  • 이클립스에서 자동으로 생성됨

 

 

 

ex) 유효한 날짜 인지 검사하는 프로그램

 

🍀BirthDay.java

public class Birthday {
    private int day;
    private int month;
    private int year;
    
    private boolean isValid;   //초기값 false
    
    public int getDay() {
    	return day;
    }
    
    public void setDay(int day) {
    	this.day = day;
    }
    
    
    
    public int getMonth() {
    	return month;
    }
    
    public void setMonth(int month) {
    	if(month < 1 || month > 12) {
        	isValid = false;
        }else {
        	isValid = true;
        	this.month = month;
        }
    }
    
    
    public void getYear() {
    	return year;
    }
    
    public void setYear(int year) {
    	this.year = year;
    }
    
    
    public void showDate() {
    	if(isValid) {
        	System.out.println( year +"년 " + month +"월 " + day + "일 이다");
        }else {
        	System.out.println("유효하지 않은 날짜이다");
        }
    }
}

 

 

🍀BirthDaytTest.java

public class BirthDayTest {
	public static void main(String[] args) {
    	Birthday date = new Birthday();
        
        date.setYear(2022);
        date.setMonth(02);
        date.setDay(31);
        
        date.showDate();
        
    }
}

 

 

 

 

 

 

 

 

📌 정보 은닉


  •  private 으로 제어한 멤버 변수도 public 메서드가 제공되면 접근 가능하지만, 변수가 public 으로 공개 되었을 때보다 private 일 때 각 변수에 대한 제한을 public 메서드에서 제어할 수 있다.
public void setMonth(int month) {
    if( month < 1 || month > 12 ) {
    	isValid = false;
    } else {
        isValid = true;
    	this. month = month;
    }
}
  • 객체 지향 프로그램에서 정보 은닉은 필요한 외부에서 접근 가능한 최소한의 정보를 오픈함으로써 객체의 오류를 방지하는 클라이언트 객체가 더 효율적으로 객체를 활용할 수 있도록 해준다.
728x90
반응형