인터페이스의 상속
인터페이스는 인터페이스로부터 만 상속받을 수 있으며 클래스와는 달리 다중 상속이 가능하다.
interface Movable {
void move(int x, int y);
}
interface Attackable {
void attack(Unit u);
}
interface Fightable extends Movable, Attackable { }
인터페이스의 구현
인터페이스도 추상클래스처럼 그 자체로는 인스턴스를 구현할 수 없으며 자신에 정의된 몸통을 만들어주는 클래스를 작성해야 하는데 implements 키워드를 사용해서 구현한다.
// Fightable 인터페이스를 구현
class Fighter implements Fightable {
public void move(int x, int y) { }
public void attack(Unit u) { }
}
// Fightable 인터페이스의 일부 구현
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) { }
}
'Java의 정석' 카테고리의 다른 글
인터페이스의 장점 (0) | 2022.04.22 |
---|---|
인터페이스를 이용한 다형성 (0) | 2022.04.21 |
인터페이스(interface) (0) | 2022.04.19 |
추상클래스의 작성 (0) | 2022.04.18 |
추상 메서드(abstract method) (0) | 2022.04.15 |