이것이 자바다 의 Chapter08 을 참고하여 정리한 포스트입니다.

이전글
인터페이스 이론정리 - 1
인터페이스 이론정리 - 2

Chapter 08. 인터페이스 - (3)

4. 인터페이스 사용

인터페이스로 구현 객체를 사용하려면 인터페이스 변수를 선언하고 구현 객체를 대입해야 한다.

인터페이스 변수;
변수 = 구현객체;
or
인터페이스 변수 = 구현객체;

  • ex) RemoteControl 인터페이스 구현 객체인 Television 과 Audio 를 사용하려면 RemoteControl 타입 변수 rc를 선언하고 구현 객체를 대입해야 한다.
    1
    2
    3
    
    RemoteControl rc;
    rc = new Television();
    rc = new Audio();
    
  • 인터페이스는 클래스의 필드, 생성자 또는 메소드의 매개 변수, 생성자 또는 메소드의 로컬 변수로 선언될 수 있다.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    
    public class MyClass {
      //필드
      RemoteControl rc = new Television();
    
      //생성자
      MyClass (RemoteControl rc) {    //MyClass mc = new MyClass(new Television());
          this.rc = rc;
    
      //메소드
      void methodA() {
          //로컬 변수
          RemoteControl rc = new Audio();
      }
    
      void methodB(RemoteControl rc) {...}    //mc.methodB(new Audio());
      }
    }
    

4.1 추상 메소드 사용

구현 객체가 인터페이스 타입에 대입되면 인터페이스에 선언된 추상 메소드를 개발 코드에서 호출할 수 있게 된다.

  • ex) RemoteControl 의 변수 rc로 turnOn() 또는 turnOff() 메소드를 호출하면 구현 객체의 turnOn() 과 turnOff() 메소드가 자동 실행된다.
    1
    2
    3
    
    RemoteControl rc = new Television();
    rc.turnOn();    // -> Television 의 turnOn() 실행
    rc.turnOff();   // -> Television 의 turnOff() 실행
    
  • ex) RemoteControlExample.java 인터페이스 사용
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    
    public class RemoteControlExample {
      public static void(String[] args) {
          //인터페이스 변수 선언
          RemoteControl rc = null;
    
          //Television 객체를 인터페이스 타입에 대입
          rc = new Television();
    
          //인터페이스의 turnOn(), turnOff() 호출
          rc.turnOn();
          rc.turnOff();
    
          //Audio 객체를 인터페이스 타입에 대입
          rc = new Audio();
    
          //인터페이스의 turnOn(), turnOff() 호출
          rc.turnOn();
          rc.turnOff();
      }
    }
    
    실행결과
    TV를 켭니다.
    TV를 끕니다.
    Audio를 켭니다.
    Audio를 끕니다.

4.2 디폴트 메소드 사용

디폴트 메소드는 인터페이스에 선언되지만, 인터페이스에서 바로 사용할 수 없다.
디폴트 메소드는 추상 메소드가 아닌 인스턴스 메소드이므로 구현 객체가 있어야 사용할 수 있다.

  • ex) RemoteControl 인터페이스는 setMute() 라는 디폴트 메소드를 가지고 있지만, 이 메소드를 아래처럼 호출할 수 없다.
    1
    
    RemoteControl setMute(true);
    
  • setMute() 메소드를 호출하려면 RemoteControl 의 구현 객체가 필요하다.
    Television 객체를 인터페이스 변수에 대입 후 setMute() 를 호출 할 수 있다.
    1
    2
    
    RemoteControl rc = new Television();
    rc.setMute(true);
    

디폴트 메소드는 인터페이스의 모든 구현 객체가 가지고 있는 기본 메소드다.
하지만 어떤 구현 객체는 디폴트 메소드의 내용이 맞지 않아 수정이 필요할 수 있다.
구현 클래스를 작성할 때 디폴트 메소드를 재정의(오버라이딩) 해서 자신에게 맞게 수정하면 디폴트 메소드가 호출될 때 자신을 재정의한 메소드가 호출된다.

  • ex) Television 과 Audio 중 어떤 객체가 인터페이스에 대입되느냐에 따라 setMute() 디폴드 메소드의 실행 결과는 달라진다.
    Audio.java 구현 클래스
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    
    public class Audio implements RemoteControl {
      //필드
      private int volume;
      private boolean mute;
    
      //turnOn 추상 메소드의 실체 메소드
      public void turnOn() {
          System.out.println("Audio를 켭니다");
      }
      //turnOff 추상 메소드의 실체 메소드
      public void turnOff() {
          System.out.println("Audio를 끕니다.");
      }
      //setVolume() 추상 메소드의 실체 메소드
      public void setVolume(int volume) {
          if(volume > RemoteControl.MAX_VOLUME) {
              this.volume = RemoteControl.MAX_VOLUME;
          } else if(volume < RemoteControl.MIN_VOLUME) {
              this.volume = RemoteControl.MIN_VOLUME;
          } else {
              this.volume = volume;
          }
          System.out.println("현재 Audio 볼륨 : " + this.volume);
      }
    
      @Override
      //디폴트 메소드 재정의
      public void setMute(boolean mute) {
          this.mute = mute;
          if(mute) {
              System.out.println("Audio 무음 처리");
          } else {
              System.out.println("Audio 무음 해제");
          }
      }
    }
    
  • RemoteControlExample.java 디폴트 메소드 사용
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
    public class RemoteControlExampel {
      public static void main(String[] args) {
          RemoteControl rc = null;
    
          rc = new Television();
          rc.turnOn();
          rc.setMute(true);
    
          rc = new Audio();
          rc.turnOn();
          rc.setMute(true);
      }
    }
    
    실행결과
    TV를 켭니다.
    무음 처리합니다.
    Audio를 켭니다.
    Audio 무음 처리

4.3 정적 메소드 사용

인터페이스의 정적메소드는 바로 호출이 가능하다.

  • ex) RemoteControlExample.java 정적 메소드 사용
    1
    2
    3
    4
    5
    
    public class RemoteControExample {
      public static void main(String[] args) {
          RemoteControl.changeBettery();
      }
    }
    

🌞 정보 : 공부 기록용 블로그입니다. 오타나 내용 오류가 있을 경우 알려주시면 감사하겠습니다.

댓글남기기