반응형
인터페이스 (interface)
- 프로그램과 사용자를 중간에서 연결하는 매개체.
<java의 interface?>
- class와 class 사이를 연결해주는 매개체.
- 프로그램과 프로그램을 연결해주는 매개체

- interface는 class가 아니므로, 일반변수/일반method 사용할 수 없다.
- 변수는 자동으로 public static final이 된다.
- 상수와 추상method가 주된 사용처(80-90%)... default method

❓ 만약 Animal(부모클래스)와 Tiger/Cat/Dog(자식클래스)로 구성되어 있을 때 Dog에만 포유류 기능을 추가 하고 싶다면 어떻게 해야할까
- 포유류를 interface로 정의하고 Dog에서 포유류를 상속받는 형태로 사용이 가능하다. (아래와 같은 형태)
- class Dog extends Animal implements 포유류{...} <- 이렇게 사용하면 된다.
<interface의 기능>
1. 다중상속가능 (,로 연결하여 사용한다)
- interface를 여러개 동시에 상속가능하다.
- 단, 상속받은 모든 인터페이스안의 추상method는 모두 오버라이딩되어야 한다.
2. method 명세서

- java의 interface는 객체의 사용방법을 정의한 타입(메서드 명세서)으로 객체의 교환성을 높여준다. 따라서 다형성을 구현하는 매우 중요한 역할을 한다.
- 개발자가 interface의 추상method를 실행시키면 class에 실질적으로 구현되어있는 method가 실행된다.
- => MainClass에서 사용하는 method는 interface로 참조 -> 실질적으로 Basic Class에서 구현한 기능을 실행한다.
- interface에는 method의 틀만 있기 때문에 class에서 사용되는 method의 명세서라고 볼 수 있다.
3. interface와 비슷한 class를 만들 수 있다. (활용가능)

➰ Quiz - My PlayList (interface를 활용하여)
🔆 SongList Interface
package quiz15;
public interface SongList {
public void insertList(String song);
public void playList();
public int playLength();
}
🔆 MelonMusic Class
package quiz15;
public class MelonMusic implements SongList{
private String[] list = new String[100];
private int count = 0;
/*
* SongList인터페이스를 상속받아서 기능을 구현합니다.
* insertList() 는 list배열에 순서대로 저장
* playLength() 는 저장된 음악의 개수를 반환
* playList() list의 음악을 랜덤하게 출력
*/
@Override
public void insertList(String song) {
list[count++] = song;
}
@Override
public void playList() {
int random = (int) (Math.random() * 100);
if(list[random] != null) {
System.out.println(list[random]);
return;
}
playList();
}
@Override
public int playLength() {
int count_list = 0;
for(int i = 0; list[i] != null; i++) {
count_list++;
}
return count_list;
}
}
🔆 Main에서 사용.
package quiz15;
public class Main {
public static void main(String[] args) {
System.out.println("============This is Melon============");
MelonMusic m = new MelonMusic();
m.insertList("Dangerously - Charlie Puth");
m.insertList("Hype boy - NewJeans");
m.insertList("After LIKE - IVE");
m.insertList("새삥 (Prod. ZICO) (Feat. 호미들) - 지코 (ZICO)");
m.insertList("Attention - NewJeans");
m.insertList("Shut Down - BLACKPINK");
m.insertList("Pink Venom - BLACKPINK");
m.insertList("Rush Hour (Feat. j-hope of BTS) - Crush");
m.insertList("Cookie - NewJeans");
m.insertList("FOREVER 1 - 소녀시대 (GIRLS' GENERATION)");
System.out.println("내 플레이 리스트에 저장된 곡 수: " + m.playLength()+"곡");
System.out.println();
System.out.println("♫random play song♫");
m.playList();
}
}
🔆 결과값
============This is Melon============
내 플레이 리스트에 저장된 곡 수: 10곡
♫random play song♫
새삥 (Prod. ZICO) (Feat. 호미들) - 지코 (ZICO)반응형
'국비지원 > JAVA' 카테고리의 다른 글
| [JAVA] 19. API (Application Programming Interface) (0) | 2022.10.18 |
|---|---|
| [JAVA] 18. 예외처리문법 (try-catch 와 try-catch-finally / throws 와 throw keyword) (0) | 2022.10.17 |
| [JAVA] 16-2. singleton Design Pattern & final Keyword & 상수(constant) & abstract (0) | 2022.10.13 |
| [JAVA] 16-1. 정적제한자 static (0) | 2022.10.13 |
| [JAVA] 15. 다형성 (0) | 2022.10.12 |