 This video was brought to you by my patrons. Thank you so much for your support. Hello there! Following the series where we are exploring object-oriented design applied to Godot engine today We'll talk about inheritance another type of relationship between true classes. So let's get started Inheritance is quite a simple concept, but it's very powerful So let me open the help here to show you how pretty much it works So here we have a let me close this we have a lot of classes So as you can see we have some base types like the integral float boolean And these are building blocks for other classes like if you see here, we have the integral building type If you go up here, we have object which is the base class for all known building types So if we open it, we have the nodes. We have reference and other classes here And I will just go to nodes here just so we can understand how inheritance works So we have node and this node inherits from object and we have in Spatial and spatial inherits from node and we have canvas item and canvas item inherits from node So the idea of inheritance is that you can have a class and it's children So object is the parent class of node for instance and node is the parent class for spatial and For canvas item and animation player and so on. What does this mean? What does a parent class means? Well, the children classes I will open node here We'll have the same capabilities of its parent So we have here the node class which inherits from object and is inherited by these other classes here We have some members that is properties of node class We have some methods which are functions that this class does But we can also have access to all an object offers as well. So if we go to the object class We also have access to all of these methods here in the node class And if we go to let's say canvas item We have access to everything that node and object offers plus what canvas item offers And if you go to the node 2d, we have everything that canvas item offers plus what node offers plus what object offers Plus what node 2d offers. So this is the idea of inheritance You can create another class that inherits the properties and the functions the methods of the parent class Researching a bit. I find a very cool I think that this is even better way to refer to inheritance because Inheritance implies that you have as I said, you have a parent class and you have a child class and this can pretty much Implies that we can have two parents, right? Because in nature a child has two parents, right? The mom and the daddy But if we think about it, actually, it's very standard that Multiple inheritance is very very dangerous. Even Godot doesn't allow you to have multiple inheritance Exactly because this is very very dangerous So the concept that I'm talking about that I think that is even better than inheritance It's the same it applies to the same concept. It's basically the same thing, but is better It doesn't imply that you can have two parents like mom and dad and the concept is generalization generalization is a mechanism for combining similar classes of objects into a single more general class and In my opinion, this is a way better way to explain inheritance because in a position of generalization we have Specialization which is when a child class inherits from a parent class is the reverse process of generalization meaning that you create a new subclass from an Existing one and to me these two concepts are way better to understand how inheritance work And I think that it makes more sense to be honest because you can't be a Specialization of two things, right? You can only be a specialization of one thing So let's see how this applies to Godot, right? First, we'll see specialization then we'll see generalization Okay, here we have two classes one is the fade out rect and note How I will explain this to you because words are very powerful fade out rect is a color rect Specializing in fading out so fading out is when you transition from Transparency to black and it uses a Animation player. So note how I say that I fade out rect is a color rect So this implies that it inherits from a color rect So if we see here, we have a fade out rect, which is type of color rect So it inherits from a color rect. Okay, so fade out rect is a color rect That uses an animation player when we say use we are implying that it aggregates another class So if you don't know about aggregation yet Look at the the video that is popping the card on your screen because I already talked about aggregation So let's go from start again fade out rect is a color rect which uses an animation player that has Fade out animation. So this animation fades from transparent to black if we play here and If we go here in the fade in rect the fading rect is a color rect Which uses an animation player that has a fading animation? So the fading animation is an animation that Animes the color from black to transparent Okay, so this can be two Specializations of a color rect. So these nodes these classes here are two color rects Specialized on fading, right? But we as you see we have a very common behavior that basically just change the values of the behavior, right? We can pretty much say that we just have a single behavior with different directions So we can use a generalization to combine these two behaviors into a more general one So since these two behaviors the fading and fade out are very similar We can create a general class that generalize this behavior. So let's create a fade color rect, right? So I will create a new scene here. I'll go here into Color rect and note that we are specializing a color rect as well because we are using a color rect That will be specialized in fading. So color rect We are already inheriting a color rect, right? And I will go here in layout for rect make this black and This color rect will have it will aggregate an animation player So color rect will aggregate an animation player and this will be a fade rect Okay, so let's save this as fade rect Let's create an animation here. Actually, I will go here and save this fade in animation as Just fade animation fade Okay, so the fade rect is a color rect that uses an animation player with a Fade animation. So this is the animation that does So in goto engine, you can use a script to specialize a node to create an inherited node So let's go here add a new script and you can see that it inherits color rect So let's create this and you see that this Extends the color rect Functionalities the color rect capabilities so we have access to everything that the color rect has and we will Specialize it in fading. So since it will be specialized in fading. Let's create a fade Function here that will have two types, which by default it will be in So let's create a num here num fades in and out And if it is let's try to match the type here Match type if it is in we will go to the animation player and play the fade animation and if it is out We'll go to the animation player and play it backwards because this is basically what a fade out is, right? It plays a fade animation backwards. Here we have a specialized color rect Which basically is specialized in fading. So let's save this and Now we have a general fade class general fading node The other way that you can extend a class that you can create a specialized class is by using the scene Your inherited scene. So let's say we want this fade rect to fade from time to time Okay, so it already has the the capability of fading and we want it to fade based on time So it will be a timed fade rect. For instance, let's create a new scene new inherited scene a Fade rect and we can also take rid of these classes here because we already have a general fade class So let's take rid of them. We don't need them anymore fade out as well We just have a fade class and let's create a timed fade now So let's close this don't save Don't save and Now we have a fade class. Note that we can't delete anything from a Inherited scene because this is the purpose, right? You don't want to delete anything that is from the base class because you are Extending it. You're not modifying it and let's add a time Timer here from time to time Let's say from from one second to another two seconds. It will start fading. Okay, so let's connect this time out to this fade function connect and We will say that this will be timed fade rect and Since we are aggregating a timer We can also modify these parameters as well. So we can have different wait times. I just add this one because I Want to test with two seconds, but you can use any wait time you want This is just an example, right? So let's say we have now a time it fade rect and if we test this It will pass two seconds and it will fade in so it will make it again And again And again, okay, and the fade rect is just a rack that can fade in and out Right. So if you go here in the ready We can tell it to fade Out as well. So let's try to test this and here we have it fades out Let me change this to fades because this makes more sense this this So this is inheritance generalization and specialization These are basically the same concepts But for me personally Generalization and specialization makes a lot of sense makes more sense than just inheritance because as I say It may imply that the class can have more than one parent, you know And for this video, this is it if you like this kind of content leave a thumbs up I have to ask this more. I need engagement here in the channel. If you are not subscribed yet subscribe below Don't forget subscribe I will make more content about this and if you really want to get more content from me If you really appreciate my work and what I'm doing here the best way you can support me is becoming one of my Patients and by the way, let's welcome Andrew school welcome Andrews and thank you so much for joining us This is it. Thank you so much for watching keep developing and until the next time