See all glossary termsInheritance is a process through which a class gets access to all the member functions and variables of another class. We say that it "extends" it.
In Godot, with the GDScript programming language, we can make a script extend another script or a class built into Godot using the extends
keyword.
We describe the relationship between the script and the class it extends as a parent-child relationship. The parent class is the one extended by the child. The child class inherits from the parent.
For example, when you begin a script with the line extends Area2D
, the parent class is Area2D
, and the child class is the script.
With inheritance and this extends
keyword, the child script literally extends and gets access to all the code provided by the parent class, Area2D
, in this example. It's as if the script contained all the code of Area2D
plus everything in the script.
There is a long chain of inheritance in the Godot game engine. For example, the Area2D
node extends CollisionObject2D
, which extends Node2D
, CanvasItem
, Node
, RefCounted
, and Object
.
So, the Area2D
node has access to all the functions and properties of Node2D
, CanvasItem
, Node
, RefCounted
, and Object
. You can see a fair part of it in the Inspector dock: the sections tell you which classes each property comes from.
The inheritance chain is why all the nodes in Godot have access to the add_child()
and get_node()
functions, among others. These two functions are part of the Node
class in the engine, and every node type inherits from it.See Also
Related terms in the Glossary
school.gdquest.com - 0.3.5-2024-12-22T12:35:35.516Z-