 This video was brought to you by my patrons. Thank you so much for your support Hello there Continuing the series of the four object oriented design pillars today We'll talk about inheritance a very powerful tool and as such we should use it carefully So in this video we'll talk about how we can use inheritance not only on gd script But also with the scene inheritance feature in Godot engine and we'll also talk about how inheritance is a powerful Communication tool so not only for programming language, but also for human language as well So let's get started. So before we dive into the implementation here in Godot engine Let's take a look on how inheritance is described is often described using the book we are using for this series Diving to design patterns. I will go to the book now and Here we can see that inheritance is described as the ability to build new classes on top of existing ones The main benefit of inheritance is code reuse because you can make a class and then if you want to make a slightly modified class you just need to inherit it and apply the new behaviors or Change the previous behavior just a little bit the downside of that as it's described right below it is that you create a Hard coupling between the inherited class and its parent class you can't high methods You have to implement the whole abstraction of the previous class and this new class will have the same Interface you have to keep that in mind that once you inherit a class you will also be tight Copied to the parent class so changes in the parent class will reflect in the child class as well This can be used for good, but this can create some bugs as well So you have to keep that in mind, but before we go back to Godot engine Let me elaborate a bit on how inheritance is a very powerful tool for human communication as well So it's a very powerful communication tool for human language once you describe a concept Let's say we can use this example here as well So once you describe what is an animal and what you consider There is the traits of an animal and you say that a cat is an animal The cat is building upon the concept of an animal as well So everything that an animal has the cat will have as well and it will have more So once you have that tool that you can reflect back on another concept You can create more and more specializing words to communicate more and more precisely what you want to other people You can keep building up upon this concept So inheritance is a very powerful language tool for human communication as well It's true inheritance that we can build complex vocabularies for other human language as well So we can create atomic pieces of definitions for concepts of the world And then we build upon these atomic pieces and then we create a very rich and complex set of words So a very rich vocabulary So keep that in mind when you are talking about inheritance because it's not just a programming language feature It is a concept that you can use when you are communicating and programming language To me is a way to communicate as well It's a way to communicate your thoughts your solutions for a given problem. So now that's safe. Let's go back to good old engine So going back to good old engine will start by extending this script So by extending when we say we will extend a class you can understand that we will inherit this class Okay. So if we right click on a note that has a script here on the scene tree We have this extend script feature here this extend script button So we will extend this as being remember now we will create a concrete class. So let me close here Currently we have just an abstraction of this class The sliding panel. So we don't have anything implemented yet And now we will inherit this abstraction So this interface to create a concrete class of a sliding panel, okay So I will go here extend script and now I will just call it sliding script You don't need to call concrete sliding panel. We can just call it the class it is A depth strike prepending is just communicate that this is an abstraction So it will help others understand what this class will be But this is a concrete one. So let's create this as a sliding panel Remember that we have here. Let me close this and this remember that we have here this virtual function So let me double that abstraction free mode this virtual function that This is a virtual method and shall be overridden by shooting classes And now we have a shooting class. So let's override it and to do that Since this is a class that inherits from the abstract panel Inheritance carries every concept that the previous class or the parent class has as well So everything that we have here will be available to this class as well So the properties and also the methods we will use this method And I will go ahead just a little bit on this series because what we will do is that we will Create a inheritance class and we'll use polymorphism to kind of override this method This is a concept that we'll talk about later But just keep in mind that we will do a polymorphism here. So I will call the slide method Now I can overread it because since this is a method that is on the parent class If I change the block of this function, so if I change the content of this method It will override the method of the parent class But with that we already have our sliding panel or concrete sliding panel But now going back to the scene we currently have this abstract sliding panel seen And if we go here, this is what we have So we have the concrete script on an abstract scene. This is not what we want So I will actually drag the abstract script to this So we have only this abstract class here And instead we will inherit this scene and create the concrete scene of this sliding panel So to do that we'll go here to scene, the inherited scene and we use this abstract sliding panel scene here And then we can call it just sliding panel and now I can drag the sliding panel script to it So this one we just created And you can see that we have this grayed out Because these are children of the previous class so of the parent class We cannot modify them so we cannot delete them for instance You can't operate on notes the current scene inherits from Remember inheritance builds upon previous concepts So you can change the previous concept. It is building upon it. It will specialize this concept So you can create more, but we can create less you can remove features from the previous class This is the concept and with this concrete sliding panel What we can make is that here we have an abstract sliding panel seen This is why this is just a square and the Direction and the values here doesn't make quite sense But here we will actually create our panel And now we have an inherited scene from an abstract scene And we have a concrete scene that inherits from this abstract scene And now we can start to tweak with these values. So let's say I want this duration to actually take just half a second and to have A slightly more distance to slide to so I think that 150 will be good And I will save it and going back to the sliding panel if you save this and actually test this scene Nothing happens, right? But now I want this sliding panel to execute its behavior So now I will create a way to call this method to call the sliding method And I can use this ready signal a observer pattern that girl has if you don't know the observer pattern I will link in the card A link to a video that I have on the design patterns and specifically on the observer pattern And what this will do is that when this scene is completely Loaded and it is inside the the scene tree It will call this signal and I will connect this signal to this light method Connect so now if we open this script, we are using the abstract version of this script So now I will drag the Concrete version of this script So now we have actually a behavior to work with And if I play the scene you can see that the sliding panel Will slide so now we have a concrete class based on an abstract class We have a concrete scene based on a abstract scene and we can keep building upon that So that's it for inheritance. You can see how powerful this tool is So don't overuse it as I said it has its downsides But you can see how this is a very powerful tool that you can use to build your library of code On the next video, we will see how we can use polymorphism to override some functions Keeping the the previous behavior and also adding more behavior to it So we'll see how we can use polymorphism to that I'd like to thank Greg pen father for becoming one of my newest patrons. Thank you Greg so much for your support I appreciate that And if you as Greg like what I'm doing here and want to support my work Consider becoming one of my patrons. You will have some exclusive perks and rewards But this is not the only way to support me You can also leave a thumbs up so I can know that we are going towards the right direction You can subscribe to the channel. This is very important So you can get more of my content as well And you can also share this video and comment below if I said something that you not quite agree Or if you know that is wrong I will be more than happy to be corrected and to know what is the right way So that's it. Thank you so much for watching. Keep developing and until the next time