interface 인터페이스 이름 {
public static final 타입 상수이름 = 값; // 상수. iv, cv 는 불가능
public abstract 메서드이름(매개변수목록); // 추상 메서드
}
예제 - public, abstract, static, final 생략 가능. 안 써도 항상 public, 상수, 추상 메서드 이므로!
interface PlayingCard {
public static final int SPADE = 4;
final int DIAMOND = 3; // public static final int DIAMOND = 3;
static int HEART = 2; // public static final int HEART = 2;
int CLOVER = 1; // public static final int CLOVER = 1;
public abstract String getCardNumber();
String getCardKind(); // public abstract String getCardKind();
}
// 구현부가 비었지만 상속 했으므로 멤버가 2개
interface Fightable extends Movable, Attackable { }
interface Movable{ void move(int x, int y); }
interface Attackable { void attack(Unit u); }
class 클래스이름 implements 인터페이스이름 {
// 인터페이스에 정의된 추상메서드를 모두 구현해야 한다.
}
예제
interface Fightable {
void move(int x, int y);
void attack(Unit u);
}
// Fighter 클래스는 Fightable 인터페이스를 구현했다.
class Fighter implements Fightable {
public void move(int x, int y) { /* 내용 생략 */ }
public void attack(Unit u) { /* 내용 생략 */ }
}
abstract class Fighter implements Fightable {
public void move(int x, int y) { /* 내용 생략 */ }
//public abstract void attack(Unit u) // 보이지 않지만 상속의 결과로 생략되어 있는 것
}
Q. 인터페이스란?
Q. 인터페이스의 구현이란?
Q. 추상 클래스와 인터페이스의 공통점은?
Q. 추상 클래스와 인터페이스의 차이점은?