Files
godot-website/collections/_release_4_5/entry-scripting-gdscript-abstract-classes-and-methods.md
Adam Scott 5387797774 Godot 4.5 release page
Huge thanks to JohnVeness, AThousandShips, and Meorge for their
great help rewording my broken English.
2025-09-15 16:03:40 -04:00

58 lines
1.9 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
type: entry
section: scripting
subsection: gdscript
rank: 1
importance: 3
anchor: abstract-classes-and-methods
title: Abstract classes and methods
blockquote: U Cant Touch This
text: |
You can now declare GDScript classes to be abstract. Declaring a class abstract means that the class is not meant to be instantiated directly. That means that you can prevent instances of a class, lets say, ``@[Animal](usertype)``, that doesnt have any purpose on its own other than to be extended by "concrete" classes like ``@[Cat](usertype)`` and ``@[Dog](usertype)``.
Abstract classes can also have abstract methods. This means that the method must be implemented in any class that extends it.
<figure class="file">
<figcaption>animal.gd</figcaption>
```manual
@[@abstract](gdscript-annotation)
@[class_name](keyword) Animal @[extends](keyword) @[Node](enginetype)
@[@abstract](gdscript-annotation)
@[func](keyword) @[cry](function)@[&lpar;&rpar; ->](symbol) @[void](basetype)
```
</figure>
<figure class="file">
<figcaption>cat.gd</figcaption>
```manual
@[class_name](keyword) Cat @[extends](keyword) @[Animal](usertype)
@[func](keyword) @[cry](function)@[&lpar;&rpar; ->](symbol) @[void](basetype)
@[# Must be implemented, otherwise an error will be thrown.](comment)
@[print](function)@[&lpar;](symbol)@["Meow!"](string)@[&rpar;](symbol)
```
</figure>
contributors:
- name: Aaron Franke
github: aaronfranke
- name: Danil Alexeev
github: dalexeev
- name: Ryan Brue
github: ryanabx
read_more:
https://github.com/godotengine/godot/pulls?q=is%3Apr+is%3Amerged+67777+106409+107717
image_alt: "Image of the Add Node window displaying the dimmed out abstract Animal
node, and its two extending classes Cat and Dog."
image_src: /storage/releases/4.5/images/gdscript-abstract.webp
image_src_2x: /storage/releases/4.5/images/gdscript-abstract-2x.webp
media_position: top
position: center-left
---