# 9.5 Interfaces

> An interface is a list of methods a class **promises** to have; it lets you write code that works with any class that keeps the promise.

Web page: https://pseudolearn.app/en/spec/objetos/interfaces

> **In one sentence**
>
> An interface is a list of methods a class **promises** to have; it lets you write code that works with any class that keeps the promise.

## 9.5.1 How it works

`Interface` declares method signatures without bodies. A class **realizes** it explicitly with `Realizes`, and must then declare those methods with the exact signature.

**Figure 9.5-1: An interface and two classes**

The interface Figura with the method Area. The classes Cuadrado and Circulo realize it. Below, Describir with the parameter T: Figura, which accepts both.

`Cuadrado` and `Circulo` realize `Figura`. `Describir` only knows the promise and accepts both.

Interfaces are not value types: no variables of an interface type are declared. They are used as a **constraint** on a generic type parameter: `T: Figura` accepts any class that realizes `Figura`.

### Program: An interface as a constraint

```pseudolearn
Interface Figura
  Method Area() As Real;
EndInterface

Class Cuadrado Implements Figura
  Define lado As Real;
  Method Constructor(l As Real)
    This.lado <- l;
  EndMethod
  Method Area() As Real
    Return This.lado * This.lado;
  EndMethod
EndClass

Class Circulo Implements Figura
  Define radio As Real;
  Method Constructor(r As Real)
    This.radio <- r;
  EndMethod
  Method Area() As Real
    Return 3.0 * This.radio * This.radio;
  EndMethod
EndClass

Subroutine Describir<T: Figura>(figura As T)
  Write "Área: ", figura.Area();
EndSubroutine

Algorithm Figuras
  Describir(New Cuadrado(2.0));
  Describir(New Circulo(1.0));
EndAlgorithm
```

**Output**

```text
Área: 4.0
Área: 3.0
```

*The program finishes normally.*

Realization is **nominal**: a class that happens to have an `Area` method but does not declare `Realizes Figura` does not satisfy the interface. A class can realize several interfaces, separated by commas, and a subclass inherits the realizations of its superclass.

> **Common mistakes**
>
> ### Program: A method that does not match the signature
>
> ```pseudolearn
> Interface Figura
>   Method Area() As Real;
> EndInterface
>
> Class Linea Implements Figura
>   Method Area() As Integer
>     Return 0;
>   EndMethod
> EndClass
>
> Algorithm Mal
> EndAlgorithm
> ```
>
> **Diagnostics**
> - `interfaceMethodSignatureMismatch` (error, 6:3): The signature of method Area does not satisfy the interface
>
> *The program finishes with an error.*

**In short**
- An interface declares signatures without bodies.
- A class satisfies it by saying so with `Realizes` and with exact signatures.
- It is used as a generic constraint, not as a variable type.

**See also**
- [3.12 Generics](https://pseudolearn.app/en/spec/tipos/genericos.md)
- [9.4 Inheritance and overriding](https://pseudolearn.app/en/spec/objetos/herencia.md)
- [7.8 Purity: Function versus Subroutine](https://pseudolearn.app/en/spec/subprogramas/pureza.md)
