Class Inheritance | Godot GDScript Tutorial | Ep 17
Summary
TLDRThis tutorial episode explores class inheritance in Godot's GDScript, explaining the concept of deriving classes to share attributes and methods. It clarifies that multiple inheritance is not supported in GDScript. The script demonstrates how to use the 'extends' keyword, the roles of base and derived classes, and the importance of constructors for variable assignment and method overriding. It showcases practical examples, including creating animal subclasses with unique behaviors, and emphasizes inheritance's role in promoting cleaner, more efficient code reuse.
Takeaways
- 📚 Class inheritance in Godot allows for creating a hierarchy of classes that share attributes and methods.
- ❌ Godot does not support multiple inheritance, meaning a class can only inherit from one other class.
- 🔑 To inherit from a class, use the 'extends' keyword followed by the class name or a file path enclosed in double quotes.
- 👉 The terms 'base class' and 'superclass' are synonymous, referring to the class whose properties are inherited.
- 🐴 The 'derived class' or 'subclass' is the class that inherits properties from the base class, such as the 'horse' class inheriting from the 'animal' class.
- 🚫 A subclass cannot have a member variable with the same name as one in the superclass; use the constructor to assign or change values from the superclass.
- 💡 The power of inheritance lies in reusing code and making small tweaks to subclasses, reducing the amount of code needed for each class.
- 🔄 Inheritance enables overriding functions in subclasses, allowing specific implementations of methods provided by the superclass.
- 🌐 Godot supports multiple layers of inheritance, where a class can inherit from another, which in turn inherits from another, and so on.
- 👶 Using constructors and class objects with inheritance can save time and lead to cleaner code, especially when deriving many classes from a base class.
- 🛠️ The video script provides practical examples, such as the 'animal', 'horse', and 'frog' classes, to illustrate inheritance concepts and their application.
Q & A
What is class inheritance in the context of programming?
-Class inheritance is a mechanism where a new class, known as the derived or subclass, is based on an existing class, known as the base or superclass. The subclass inherits attributes and methods from the superclass, creating a hierarchy of classes that share a set of functionalities.
Is multiple inheritance allowed in Godot's GDScript?
-No, in Godot's GDScript, multiple inheritance is not allowed. A class can only inherit from one other class or file.
How do you implement inheritance in GDScript?
-In GDScript, inheritance is implemented using the 'extends' keyword followed by the class name or a file path wrapped in double quotes, which turns it into a string.
What is the difference between a base class and a derived class?
-A base class, also known as a superclass, is the class from which properties are inherited. A derived class, also known as a subclass, is the class that inherits properties from the base class.
Why can't a subclass have a member variable with the same name as one in the superclass?
-A subclass cannot have a member variable with the same name as the superclass to avoid conflicts and ensure that the subclass does not unintentionally override or shadow the superclass's variable.
How can a subclass change the value of a member variable inherited from the superclass?
-A subclass can change the value of an inherited member variable by using the constructor method to assign or change the value of the variable from the subclass to the superclass.
What is the purpose of using inheritance in programming?
-Inheritance is used to promote code reuse, reduce redundancy, and create a hierarchical structure of classes. It allows for cleaner code and easier definition of classes with similar functionalities but with additional or modified behaviors.
Can functions be overridden in GDScript?
-Yes, in GDScript, you can override functions from the superclass in the subclass to provide a specific implementation of a method that is already provided by the superclass.
What does it mean to override a function in a subclass?
-Overriding a function in a subclass means providing a new implementation for a method that already exists in the superclass, allowing the subclass to have its own specific behavior for that method.
Can you have multiple layers of inheritance in GDScript?
-Yes, GDScript allows for multiple layers of inheritance. A class can inherit from another class, which in turn can inherit from another, creating a chain of inheritance where a subclass can access methods and variables from all its ancestor classes.
What are some tips for using inheritance effectively in GDScript?
-Effective use of inheritance in GDScript includes using it for cleaner code, reusing code from a base class, and making small tweaks for individual subclasses. It's also important to use constructors and class objects to pass down values and change them as needed.
Outlines
📚 Introduction to Class Inheritance in Godot
This paragraph introduces the concept of class inheritance in Godot, a game engine's scripting language. It explains that inheritance allows for the creation of a hierarchy of classes that share attributes and methods. The tutorial clarifies that multiple inheritance is not supported in Godot's GDScript. The paragraph also defines base and derived classes, also known as superclass and subclass, and their roles in inheritance. An example is provided with an 'Animal' class and a 'Horse' class, illustrating how the 'Horse' class inherits properties from the 'Animal' superclass. The importance of constructors in modifying inherited properties is highlighted, and the limitations regarding member variables with the same name are discussed.
🐎 Utilizing Inheritance for Code Reusability
The second paragraph delves into the practical applications of inheritance, emphasizing its role in promoting cleaner and more efficient code. It uses the 'Animal' base class as an example to demonstrate how different animal types can inherit from it, thus avoiding repetitive code. The paragraph explains the concept of overriding functions in subclasses to provide specific implementations, as opposed to using the superclass's method. It also touches on the possibility of multiple layers of inheritance, where a class can inherit from another class, which in turn inherits from yet another class, thereby accessing a broader range of methods and variables. An example of a 'Node' class, an 'Animal' subclass, and a 'Horse' subclass illustrates this concept.
🐸 Inheritance Best Practices and Code Efficiency
This paragraph focuses on best practices for using inheritance in coding, particularly in the context of Godot's GDScript. It underscores the efficiency gains from using inheritance when creating multiple classes with similar codebases. The 'Frog' and 'Horse' class files are used as examples to show how inheritance reduces the amount of code needed. The importance of constructors in passing values to base classes is reiterated, and the paragraph provides a step-by-step guide on how to implement inheritance in practical scenarios, including how to handle member variables and function overriding.
🔚 Wrapping Up the Inheritance Tutorial
The final paragraph serves as a conclusion to the tutorial on class inheritance. It summarizes the key points discussed in the previous paragraphs and encourages viewers to download a GitHub file provided in the description to practice inheritance. The tutorial aims to improve understanding and application of inheritance in programming projects. The host expresses gratitude for the viewers' engagement and looks forward to their participation in the next episode.
Mindmap
Keywords
💡Class Inheritance
💡GDScript
💡Base Class
💡Derived Class
💡Constructor Method
💡Member Variables
💡Override
💡Multiple Layers of Inheritance
💡Class Objects
💡Code Reusability
Highlights
Introduction to class inheritance in Godot's GDScript, emphasizing its role in creating a hierarchy of classes that share attributes and methods.
Explanation of the 'extends' keyword used to inherit from another class, with the option to use a class name or a file path.
Clarification that multiple inheritance is not allowed in GDScript.
Definition of base (super) class and derived (sub) class in the context of inheritance.
Demonstration of how a subclass can use member variables and methods from the base class, with an example of the 'Animal' and 'Horse' classes.
Important note on avoiding variable name conflicts between a subclass and its superclass.
Use of the constructor method to assign or change the value of a member variable inherited from the superclass.
The power of inheritance to allow multiple subclasses to inherit from a single base class, as illustrated with the 'Horse' and 'Frog' classes.
How to pass values to the base class from the subclass using the constructor method.
The ability to add variation to objects by passing parameters in the subclass's initialize function.
Inheritance's role in promoting cleaner code and reusing code from a base class to avoid redundancy.
The concept of overriding functions in GDScript, with an example of the 'eat' function being overridden in the 'Horse' class.
Explanation of multiple layers of inheritance in GDScript, allowing a class to inherit from another class that itself inherits from another.
Practical tips on using inheritance effectively with class constructors and objects, especially when deriving many classes from a base class.
Code examples provided to illustrate the concepts discussed, including the 'Animal', 'Horse', and 'Frog' classes with inheritance and overriding.
Final thoughts on the importance of inheritance in simplifying code and allowing for specific implementations in subclasses.
Invitation to download the GitHub file for further practice with inheritance in GDScript.
Transcripts
hello and welcome to another episode in
the GT script fundamental tutorial
series in this episode we will be
talking about class inheritance now to
go over the definition again a class
inheritance is a mechanism where you can
derive a class from another class for a
hierarchy of classes that share a set of
attributes and methods in Godot you can
inherit from the following a global
class another class file and an inner
class inside another class file one
thing to keep in mind is that in GT
script multiple inheritance is not
allowed now to inherit from a class
simply use the extends keyword followed
by a class name or a file path now keep
in mind when you decide to use a file
path you need to wrap it in double
quotations essentially turning it into a
string now when you do inheritance you
deal with two things a base class and a
derived class a base class can also be
referred to as a superclass and a
derived class can also be referred to as
a subclass now a subclass is the class
that inherits properties from another
class the other class which is referred
to as the superclass a super class is
the class whose properties are inherited
by subclass the word property is just a
broad concept basically it encompasses
attributes such as class members and
methods and relationships to other
classes such as inherited classes now
when you inherit a derived class has the
ability to use member variables and
methods from the base class let's go
ahead and take a look at an example as
you can see here we have our classes an
animal file or rather an animal class
and we have a horse class now the horse
class is the derived class and the
animal class is the base class so animal
is the superclass and horse is the
subclass our base class or rather the
superclass has a function called eat
which just prints out a statement to
console and it has a member variable
food one thing to keep in mind is that
the horse class cannot have a member
variable with the same
named as food now if you try in your
horse class to declare a member variable
called food you will throw an error and
the error you will receive is a variable
already exist error to get around that
you use the constructor method and from
the horse class you can assign or change
the value of a member variable from your
superclass in this case the animal class
so as you can see here in our
constructor method in horse class we are
assigning the food with the value grass
now what this does is we change the
value from our derived class to the
superclass and now if we were to access
the method eat what we will get is
instead of saying eats something what we
will print out instead is eat grass now
normally for something as simple as this
you wouldn't go ahead and use
inheritance you would probably use class
objects basically class instantiation
however the real power of inheritance
comes from the fact that you can have
multiple classes as subclass and they
can all inherit from a single base class
so for example we have a horse class and
through the constructor method again we
are assigning food to the string value
grass and in our new class file called
frog we're passing a value bugs into our
food variable again through the
construct method we'll take a better
look at that through code examples later
now if you want to pass a value to your
base class from your subclass all you
have to do is simply call the
initialized function in your subclass so
in this example we're in our horse class
and after the initialized function we
use the dot notation
followed by a value inside the
parentheses we want to pass to our
superclass in this case every time we
create a horse class object what we're
going to do is initialize our horse
class with the grass literal value and
pass that to our food variable if we
want to actually use this class for
example in a class object all you have
to do is simply create your standard
variable followed by the class
name in this case horse followed by the
new method now when we create a class
object like this we're going to assign
the grass variable and that's no fun
what if you want to add variation to to
your objects well to do that instead of
passing a literal value inside the
parentheses what you'll do is in your
subclass in the initialize function what
you're going to do is receive a
parameter or rather use a parameter and
then you take that parameter you just
created in this case our parameter name
is value and we pass that value into the
parentheses after it so in this case
anishka sees value dot parentheses
values and now when we create a class
object as you can see here when we call
the new method and we pass let's say a
literal value grass what we do is we end
up in the constructor method and now
grass is our value and we simply pass
that value to our superclass and so now
our superclass has the literal value
grass and can do whatever it needs to do
in the case of our simple example the
base class will just assign the food
variable the literal string value grass
now why exactly is inheritance important
well for one thing inheritance allows
for cleaner code it makes it easier for
you to define a class especially when we
want to make additional tweaks to a
class now basically you use inheritance
when you want to reuse code from a class
take for example our base class animal
what if your game needs 20 different
types of animals instead of writing
similar code in each class what you can
do is create one base class one source
of truth and from there your animals can
derive from it can it or rather they can
inherit from that base class and then
you can just make small tweaks so
instead of writing for example 100 lines
of code in each class you can cut that
down to maybe 10 or 20 depending on what
you're adding to each individual class
or rather subclass the real power from
inheritance is the ability to override
now in Gd script you are only allowed to
override functions
you cannot override variables now
overriding allows a subclass to provide
a specific implementation of a method
that is already provided by its
superclass take this for example we have
our animal class with the eat function
and we have our horse class if we use
the eat function what we're going to do
is call the eat function inside our base
class in this case we're gonna print out
eats food but what if you want something
else what if you want something
different well in your horse class file
what you do is you write it out in the
code in this case your eat function and
then you just write whatever you want in
this case we're going to print out loves
to eat hit to the console now every time
you call the eat function instead of
calling the eat function in your base
class what you do is you end up calling
the eat function in your current class
in this case the horse class file now an
important thing to note is that in Gd
script you can have multiple layers of
inheritance I mean by definition
inheritance implies that you can you can
have layers so let's take a look at an
example let's say your superclass is the
node class and then your animal class
inherits from the node class and your
horse class inherits from the animal
class well your horse class will now
have access to not only the virtual
methods in your node class but it will
also have access to methods and
variables in the animal class let's go
ahead and take a look at this example as
you can see here we have the no class or
base class we have the animal class the
subclass and we have the horse class
which is a subclass of the animal class
which is the subclass of the node base
class now in the animal class we extend
our node so now we have access to the
virtual methods node class provides and
we've also declared a neat function now
let's say in our horse and frog class we
extend animal well now we have access to
the virtual method ready provided from
the node class and we can also call the
eat function from the animal now some
tips about inheritance inheritance is a
powerhouse it's very powerful when you
use it with class constructors and class
objects now again it saves you a lot of
time when writing
class codes for example an animal class
especially if you're deriving a lot of
classes from your base class now again
you use inheritance when you want to
have lots of classes with similar code
but additional tweaks to their behaviors
either new or inherited methods now
let's go ahead and take a look at some
code so as you can see here we have our
class animal you can see we're using the
class name animal we have our member
variable food only takes in string
values and we have our initialize
constructor method in this case we're
allowing you to pass values down from
your subclasses and if you choose not to
pass any value down from your subclass
we are also defaulting a literal string
value something as you can see here
through this code that is exactly what
we're doing passing a value to a
parameter use that parameter value
assign it to our food and our food value
is a member variable that only accepts
string and we have one function called
eat in this case we'll print out to the
console eating something now I went
ahead and created a second class file
called frog and as you can see here we
are extending from the animal class
we're inheriting from the animal class
we went ahead and gave a name to this
class file to the Godot editor in this
case we've named it frog and notice how
all we have in this class file is the
initialize function the initialize
constructor method and we are passing a
value down to our base class in this
case the literal value bugs as you can
see here take a notice this is only
seven lines of code versus T 12 now this
doesn't seem a lot because these are
just example files but in the real or
rather in your production code your
scripts aren't gonna be only ten or
twelve lines of code they may be 50 100
maybe even 200 and instead of writing
200 lines of similar code in our frog
class we can just basically bring it
down to just the essentials in our
constructor method depending on how
complex we want to write similar code or
depending on how complex we want our
subclasses to be in this case we're just
passing through the initial method now
let's go ahead and take a look at the
horse class file in this class file we
are extending from the animal class
we're inheriting from the animal class
we're giving this a class name that will
show up in the Godot editor called horse
but notice how we're adding an
additional member variable not only do
we want the food variable that we're
going to inherit from the animal class
we also want our own variable that we
can use for this specific class now
again one thing to keep in mind if we
try to declare a variable with the same
name that we inherit from our base class
in this case the variable name called
food we're gonna throw an error it's
going to tell you this variable already
exists moving on we have our initialized
function and as you notice here not only
do we take in a value for a name that we
are going to assign to the horse class
member variable called first name but we
also want to take in a value for the
food variable we call it food and we
want to pass it down to our base class
so our base class can use it in that
case the base class is going to print
out eats orange if we don't pass a value
when we create a class object however
check this out we have our function
called eat what we're doing through this
is we're overriding our base classes
function so our base class has a
function called eat and if we didn't
declare this function we would use that
class's method however because we're
declaring a function in this class file
when we call the function eat and for
example class object what we're going to
do is call this function and this
function is going to print out a name
followed by really wants to eat that and
then the value food and on top of that
we've also added an additional function
called sleep where we print out the name
followed by the words is sleeping now
lastly putting all of this together we
have our class object horse object and
we're going to create a new horse in
this case we're going to pass to the
class object a name darth sidious
and we're going to pass in
value for the food variable called Apple
and of course for a frog object we're
just going to create a simple frog
object now as you can see here through
our class object if we call the eat
function we're gonna print out darth
sidious really wants to eat that Apple
and when we call the sleep function
we're gonna print out darth sidious is
sleeping and when we call the frog
objects eat function the Frog object
doesn't have any function so therefore
it's gonna use the base classes eat
function and it's simply gonna put out
or print out eating bugs so again for a
more complicated horse class when you
want to pass down values and change
values use the constructor method if you
want to pass down values from classes
you've inherited you got to use the dot
notation followed by the value you want
to pass make sure that data types are
acceptable and lastly if you want to
change member variables through
instantiation use the initial
construction and then you just simply
pass your variable or you simply pass
your parameter value to your member
variable one thing to note is that you
can actually access your member
variables directly from the code so even
though we have the eat function we can
also call the food member variable we
can of course go ahead and assign it a
different value well that's all I have
for you in this episode go ahead and
download the github file I put a link
down in the description below when you
download that file go ahead and play
around with inheritance the more you
practice the better you'll be at
understanding how inheritance works and
how you can use it and apply it to your
programming in your projects thank you
for joining me in this episode I look
forward to seeing you in the next
استعرض المزيد من الفيديوهات ذات الصلة
Class Objects & Constructors | Godot GDScript Tutorial | Ep 16
Java Inheritance | Java Inheritance Program Example | Java Inheritance Tutorial | Simplilearn
Classes | Godot GDScript Tutorial | Ep 15
#67 More on Interfaces in Java
Python OOP Tutorial 1: Classes and Instances
La Lógica de la Programación Orientada a Objetos explicada con Minecraft
5.0 / 5 (0 votes)