일종의 추상클래스지만 보다 추상화 정도가 높음
→ 오직 추상메서드와 상수만 멤버로 가짐
interface 인터페이스 명{
public static final 타입 상수명 = 값;
public abstract 메서드명(매개변수);
}
모든 멤버변수는 public static final 이어야 함, 생략 o
모든 메서드는 public abstract 이어야 함, 생략 o
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();
}
상속 가능
interface Movable{
void move(int x, int y);
}
interface Attackable{
void attack(Unit u);
}
interface Fightable extends Movable, Attackable{ }
implements 키워드 사용
class Fighter implements Fightable{
public void move(int x, int y) { /*내용 생략*/ }
public void attack(Unit u) { /*내용 생략*/ }
}
인터페이스 메서드 중 일부만 구현 시 abstract 붙여서 선언
abstract class Fighter implements Fightable{
public void move(int x, int y) { /*내용 생략*/ }
}
상속과 구현 동시에 가능
class Fighter extends Unit implements Fightable{
public void move(int x, int y) { /*내용 생략*/ }
public void attack(Unit u) { /*내용 생략*/ }
}