Chapter8. 인터페이스
08-1) 인터페이스
- 인터페이스는 객체의 사용 방법을 정의한 타입이다. 인터페이스의 메소드를 호출하면 객체의 메소드를 호출시킨다.
- 상수 필드와 추상메소드로 구성된다.
- 구현클래스는 반드시 추상메소드를 재정의해야한다. 클래스 선언시 implements 키워드를 사용한다.
- 클래를 선언할 때, 인터페이스는 필드, 생성자 or 메소드의 매개변수, 생성자 or 메소드의 로컬 변수로 선언될 수 있다.
[인터페이스 상수필드, 추상메소드 선언]
public interface RemoteControl {
//상수
int MAX_VOLUME = 10;
int MIN_VOLUME = 0;
//추상메소드 - 메소드 선언부만 작성
void turnOn();
void turnOff();
void setVolume(int volume);
}
[구현클래스 - implements 추가, 실체메소드 선언]
public class Television implements RemoteControl {
//필드
private int volume;
//turnOn() 추상 메소드의 실체 메소드
public void turnOn() {
System.out.println("TV를 켭니다.");
}
//turnOff() 추상 메소드의 실체 메소드
public void turnOff() {
System.out.println("TV를 끕니다.");
}
//setVolume() 추상 메소드의 실체 메소드
public void setVolume(int volume) {
if(volume>RemoteControl.MAX_VOLUME) { //인터페이스 상수를 이용해서 volume필드 값 제한
this.volume = RemoteControl.MAX_VOLUME;
} else if(volume<RemoteControl.MIN_VOLUME) {
this.volume = RemoteControl.MIN_VOLUME;
} else {
this.volume = volume;
}
System.out.println("현재 TV 볼륨: " + this.volume);
}
}
[인터페이스 변수에 구현 객체 대입]
- 인터페이스로 구현 객체 사용하려면, 인터페이스 변수를 선언하고 구현객체를 대입한다.
- 인터페이스 변수는 참조타입이기 때문에 구현 객체가 대입될 경우 구현 객체의 번지를 저장한다.
- 다중 인터페이스도 구현 가능하다.
public class RemoteControlExample {
public static void main(String[] args) {
RemoteControl rc; //인터페이스 변수 선언
rc = new Television(); //구현 객체 대입
rc = new Audio();
}
}
- 다중 인터페이스 구현클래스
- 다중 인터페이스를 구현할 때, 구현 클래스는 모든 인터페이스의 추상메소드에 대해 실체 메소드를 작성해야 한다.
[인터페이스]
public interface Searchable {
void search(String url);
}
[다중 인터페이스 구현 클래스]
public class SmartTelevision implements RemoteControl, Searchable {
private int volume;
public void turnOn() { //Remotecontrol 추상메소드에 대한 실체메소드
System.out.println("TV를 켭니다.");
}
public void turnOff() {
System.out.println("TV를 끕니다.");
}
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("현재 TV 볼륨: " + this.volume);
}
public void search(String url) { //Searchable 추상메소드에 대한 실체 메소드
System.out.println(url + "을 검색합니다.");
}
}
[인터페이스 변수에 구현 객체 대입]
public class SmartTelevisionExample {
public static void main(String[] args) {
SmartTelevision tv = new SmartTelevision(); //인터페이스 객체 생성
RemoteControl rc = tv; //구현 객체 대입
Searchable searchable = tv; //구현 객체 대입
}
}
[인터페이스 사용]
- 클래스를 선언할 때, 인터페이스는 필드, 생성자 or 메소드의 매개변수, 생성자 or 메소드의 로컬 변수로 선언될 수 있다.
public class MyClass {
//필드 - 인터페이스가 필드타입으로 사용될 경우, 필드에 구현 객체를 대입할 수 있음
RemoteControl rc = new Television();
//생성자
MyClass() {
}
MyClass(RemoteControl rc) { //인터페이스가 생성자의 매개변수 타입으로 사용될 경우,
this.rc = rc; //new연산자로 객체를 생성할 때 구현 객쳋를 생성자의 매개값으로 대입할 수 있음
rc.turnOn();
rc.setVolume(5);
}
//메소드
void methodA() {
RemoteControl rc = new Audio(); //인터페이스가 로컬 변수 타입으로 사용될 경우, 변수에 구현 객체를 대입할 수 있음
rc.turnOn();
rc.setVolume(5);
}
void methodB(RemoteControl rc) { //인터페이스가 메소드의 매개 변수 타입으로 사용될 경우, 메소드 호출 시 구현 객체를 매개값으로 대입할 수 있음
rc.turnOn();
rc.setVolume(5);
}
}
public class MyClassExample {
public static void main(String[] args) {
System.out.println("1)----------------");
MyClass myClass1 = new MyClass();
myClass1.rc.turnOn();
myClass1.rc.setVolume(5);
System.out.println("2)----------------");
MyClass myClass2 = new MyClass(new Audio());
System.out.println("3)----------------");
MyClass myClass3 = new MyClass();
myClass3.methodA();
System.out.println("4)----------------");
MyClass myClass4 = new MyClass();
myClass4.methodB(new Television());
}
}
출력결과:
1)----------------
TV를 켭니다.
현재 TV 볼륨: 5
2)----------------
Audio를 켭니다.
현재 Audio 볼륨: 5
3)----------------
Audio를 켭니다.
현재 Audio 볼륨: 5
4)----------------
TV를 켭니다.
현재 TV 볼륨: 5
08-2) 타입 변환과 다형성
자동 타입 변환
필드, 매개변수의 다형성
강제 타입 변환
인터페이스 상속 - 다중 상속 허용
'Java' 카테고리의 다른 글
220119 혼자공부하는자바 Ch10~Ch14 (0) | 2022.01.19 |
---|---|
220118 혼자공부하는자바 Ch9 중첩 클래스와 중첩 인터페이스 (0) | 2022.01.18 |
220117 혼자공부하는자바 Ch7 상속, Chp8 인터페이스 (0) | 2022.01.18 |
220115 혼자공부하는자바 Ch7 상속 (0) | 2022.01.15 |
220112 혼자공부하는자바 Ch6-5~ 모르는 부분 리마인드 (0) | 2022.01.12 |