# More on Classes

This page: https://emojicode-ide.com/en/docs/advanced/classes/

## Initializers every subclass must provide (🔑)

If you put `🔑` on an initializer, every subclass must also provide that initializer. In the subclass, you write it with `🔑` too.

```emojicode
🐇 🐾 🍇
  🖍🆕 name 🔡
  🔑 🆕 🍼 name 🔡 🍇🍉
  ❗️ 🏷 ➡️ 🔡 🍇
    ↩️ name
  🍉
🍉

🐇 🐕 🐾 🍇
  🔑 🆕 name 🔡 🍇
    ⤴️🆕 name❗️
  🍉
🍉

🏁 🍇
  😀 🏷 🆕🐕 🔤Buddy🔤❗️❗️❗️
🍉
```

Output:

```text
Buddy
```

## Methods that cannot be overridden (🔏)

If you put `🔏` on a method, subclasses can no longer override it.

Example of a compile error:

```emojicode
🐇 🐾 🍇
  🆕 🍇🍉
  🔏❗️ 🗣 🍇
    😀 🔤…🔤❗️
  🍉
🍉

🐇 🐕 🐾 🍇
  ✒️ ❗️ 🗣 🍇
    😀 🔤Woof🔤❗️
  🍉
🍉

🏁 🍇
  🗣 🆕🐕❗️❗️
🍉
```

## Visibility (🔓, 🔒, 🔐)

Write one of these before a method to decide where it can be called from.

| Syntax | Can be called from |
|---|---|
| `🔓` (or nothing) | anywhere |
| `🔐` | inside the class and its subclasses |
| `🔒` | only inside the class itself |

```emojicode
🐇 🐾 🍇
  🆕 🍇🍉
  🔐❗️ 💓 ➡️ 🔡 🍇
    ↩️ 🔤Thump thump🔤
  🍉
🍉

🐇 🐕 🐾 🍇
  ❗️ 🎧 🍇
    😀 💓👇❗️❗️
  🍉
🍉

🏁 🍇
  🎧 🆕🐕❗️❗️
🍉
```

Output:

```text
Thump thump
```

## Code that runs on cleanup (♻️)

`♻️ 🍇 … 🍉` runs when an instance is no longer used and gets cleaned up (a destructor).

```emojicode
🐇 🎈 🍇
  🖍🆕 color 🔡
  🆕 🍼 color 🔡 🍇
    😀 🔤Blew up the 🧲color🧲 balloon🔤❗️
  🍉
  ♻️ 🍇
    😀 🔤The 🧲color🧲 balloon deflated🔤❗️
  🍉
🍉

🏁 🍇
  🆕🎈 🔤red🔤❗️ ➡️ red
  😀 🔤---🔤❗️
🍉
```

Output:

```text
Blew up the red balloon
---
The red balloon deflated
```

Emojicode manages memory with reference counting, and cleans an instance up as soon as its last reference goes away ([Memory and References](https://emojicode-ide.com/en/docs/advanced/memory.md)).

## Generics on a single method

If you write `🐚Name Constraint🍆` after a method name, that method alone can take a type to use.

```emojicode
🐇 🧰 🍇
  🐇❗️ 👯 🐚T ⚪️🍆 value T count 🔢 ➡️ 🍨🐚T🍆 🍇
    🆕🍨🐚T🍆❗️ ➡️ 🖍🆕 list
    🔂 i 🆕⏩ 0 count❗️ 🍇
      🐻 list value❗️
    🍉
    ↩️ list
  🍉
🍉

🏁 🍇
  👯🐇🧰 🔤hi🔤 3❗️ ➡️ greetings
  😀 🆕🔡 greetings 🔤 🔤❗️❗️
  👯🐇🧰 7 2❗️ ➡️ sevens
  😀 🔡 📏 sevens❓❗️❗️
🍉
```

Output:

```text
hi hi hi
2
```

You don't have to write the type; it is inferred from the arguments.
