인터페이스(interface) 타입은 인터페이스를 구현하는 클래스와 상호작용하는 방식에 대한 계약을 제공합니다. 인터페이스는 인스턴스화될 수 없지만, 클래스는 인터페이스에서 상속하여 그 메서드를 구현할 수 있습니다. 인터페이스는 추상 클래스와 유사하지만 부분적인 구현이나 필드를 정의의 일부로서 허용하지 않는다는 점에서 다릅니다.
예를 들어 자전거나 말 등 탑승 가능한 것에 대한 인터페이스를 만들어 보겠습니다.
Verse
rideable := interface():
Mount()<decides> : void
Dismount()<decides> : void인터페이스를 상속하는 클래스는 인터페이스의 함수를 구현하고 오버라이드 지정자를 추가해야 합니다.
Verse
bicycle := class(rideable):
...
Mount<override>()<decides> : void =
...
Dismount<override>()<decides> : void =
...
horse := class(rideable):
...
Mount<override>()<decides> : void =
인터페이스는 다른 인터페이스를 확장할 수 있습니다. 예를 들어 탑승 가능한 것을 전부 움직이는 것도 가능하도록 지정할 수 있습니다.
Verse
moveable := interface():
MoveForward() : void
rideable := interface(moveable):
Mount()<decides> : void
Dismount()<decides> : void클래스는 인터페이스 및 다른 클래스로부터 상속할 수 있습니다. 예를 들어 말을 정의하고 탑승 가능한 안장이 있는 말과 차별화할 수 있습니다.
Verse
horse := class(moveable):
...
MoveForward()<decides> : void =
...
saddle_horse := class(horse, rideable):
...
Mount<override>()<decides> : void =
...
Dismount<override>()<decides> : void =
클래스는 여러 인터페이스로부터 상속할 수 있습니다.
Verse
lockable := interface():
Lock() : void =
...
Unlock() : void =
...
bicycle := class(rideable, lockable):
…
Mount<override>()<decides> : void =
...