In the object-orientated programming world, interfaces essentially serve as templates or abbreviated versions of classes. They provide an interface for functions, which may differ from class to class in terms of implementation, but are identical in terms of signature (i.e. function name + return value + parameter list).
As a rule, interfaces are used as soon as several classes have to behave in a certain way.
Interfaces are therefore ‘behaves like’ associations.
public interface Movable {
void move();
}
class Dog implements Movable {
...
@override
public void move() {
legs.forEach((leg) -> leg.step());
}
}
class Fish implements Movable {
...
@override
public void move() {
fin.swing();
}
}