# 9.4 Inheritance and overriding

> With `Inherits From`, a class receives the attributes and methods of another and can change how some of them behave.

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

> **In one sentence**
>
> With `Inherits From`, a class receives the attributes and methods of another and can change how some of them behave.

## 9.4.1 How it works

A **subclass** inherits from **a single** superclass. Everything the superclass has, the subclass has too, and an object of the subclass works wherever one of the superclass is expected: a `Perro` is an `Animal`.

If the subclass declares a method with the same name and signature, it **overrides** it. Every call uses the method of the object's **real** class, not that of the variable's type. This is called **dispatch**.

### Program: A method that changes in the subclass

```pseudolearn
Class Animal
  Define nombre As String;

  Method Constructor(n As String)
    This.nombre <- n;
  EndMethod

  Method Sonido() As String
    Return "...";
  EndMethod

  Method Presentarse() As String
    Return This.nombre + " dice " + This.Sonido();
  EndMethod
EndClass

Class Perro Inherits From Animal
  Method Constructor(n As String)
    Super.constructor(n);
  EndMethod

  Method Sonido() As String
    Return "guau";
  EndMethod
EndClass

Algorithm Granja
  Define a As Animal <- New Perro("Toby");
  Write a.Presentarse();
  Define b As Animal <- New Animal("Pez");
  Write b.Presentarse();
EndAlgorithm
```

**Output**

```text
Toby dice guau
Pez dice ...
```

*The program finishes normally.*

**Figure 9.4-1: Dispatch by real class**

The class Animal with Sonido and Presentarse, and below it Perro, which inherits from Animal and redefines Sonido. The object Toby is a Perro: the search for Sonido starts in Perro.

`Presentarse` lives in `Animal`, but `This.Sonido()` looks first in the object's real class: for Toby it finds the one in `Perro`.

`Super` calls the superclass version on the same object. It is only used inside a subclass, followed by a call.

> **Limit**
>
> - Only single inheritance exists: one superclass per class.
> - An override keeps the number and types of parameters, passing modes and return type. If anything changes, it is `incompatibleMethodOverride`, not overloading.
> - A subclass cannot declare an attribute with the name of an inherited one.

> **Common mistakes**
>
> ### Program: An override with a different signature
>
> ```pseudolearn
> Class Base
>   Method Saludo() As String
>     Return "hola";
>   EndMethod
> EndClass
>
> Class Hija Inherits From Base
>   Method Saludo(nombre As String) As String
>     Return "hola " + nombre;
>   EndMethod
> EndClass
>
> Algorithm Mal
> EndAlgorithm
> ```
>
> **Diagnostics**
> - `incompatibleMethodOverride` (error, 8:3): Overridden method Saludo signature does not match superclass method
>
> *The program finishes with an error.*

**In short**
- `Inherits From` gives a class everything its superclass has.
- A method with the same signature overrides; every call uses the real class.
- `Super` calls the superclass version.
- Single inheritance and no signature changes.

**See also**
- [9.2 Methods and This](https://pseudolearn.app/en/spec/objetos/metodos.md)
- [9.5 Interfaces](https://pseudolearn.app/en/spec/objetos/interfaces.md)
- [3.2 Relations between types and widening](https://pseudolearn.app/en/spec/tipos/relaciones.md)
