 This video was brought to you by my patrons. Thank you so much for your support Hello there This is the final chapter of our series on object-oriented design pillars So we already talked about abstraction, encapsulation, inheritance, and finally we'll talk about Polymorphism. So without further ado, let's get started. I will open the book that we are using in this series But the book itself doesn't give a good definition for polymorphism You see so I will open the book here And here in the book they described polymorphism as the ability of a program to detect the real class of an object And call its implementation even when its real type is unknown in the current context. This doesn't give Much to to work on right? It's not very reasonable So polymorphism simply is the ability of a child class to override the implementation of a parent class And doing this is a very powerful ability if you saw in the previous video when I use inheritance to implement an abstract class I was really using the power of polymorphism there polymorphism inheritance and abstraction are Very closed pillars Actually, all the four pillars are very well tied together So when you concrete a class by making a concrete implementation of an abstract class You are you will use polymorphism to override the previous methods the virtual methods the abstract methods of the abstract class So we can use this ability of polymorphism not only to override the parent class method But also to improve it. We can make what we call a super call So we call the method on the super class or the parent class and we can add more to it And we'll see how to do that in goto the engine now So I will open goto the engine here And here we have the sliding panel, right? And if we go here into the inspector We can see that the sliding panel inherits from control node Actually, it inherits from panel, but panel doesn't have any implementations here But the control node inherits from the canvas item class So what I want to make here is to make every other class treat this sliding panel as if they was dealing with a canvas item So what this will do is that instead of having other classes knowing that this is sliding panel has a slide method So I will open here and this is what we have, right? We have a sliding method a slide method What I want to do is that instead of having to call this is sliding panel and have more specific and more deep knowledge of what this class is Instead I will shallow this hierarchy by treating this sliding panel as if They were dealing with a canvas item and to do that I will make this sliding panel slide itself whenever it's meant to be shown So to be displayed on the screen and to Slide out of the screen whenever it's meant to hide and the way that we can do that is By using polymorphism We will override the show and the hide method from the canvas item class Since this sliding panel inherits from canvas item we can override this method But we will also make a super call So we will call the show and the hide method from the canvas item class as well And to do that we will first override this function So show and right now we are already overriding the implementation of the canvas item show method And the first thing that we will do here in the show method is to call The show method of the canvas item itself instead of calling this class implementation of the show method It will first call the parent class implementation and after that we will add The the implementation that we want in this sliding panel So I want this to set the current mode to be moved in So if we go here in the abstract sliding panel and and this is one of the coolest thing About having an abstract class is that you can always refer back to see what is available in the current class So here in the sliding panel we have the mode in and out And going back here I will set this to be in and then tell it to slide and after that I will override the Hide method But here in the hide method we will only tell it to hide after it finishes sliding So I will first tell it to be mode Out and then I will tell it to slide and I will wait for the twin To finish sliding So twin complete to wait for the twin completed signal and then we will make the super call of the hide method And we did our polymorphism here So, uh, if I create another class here if I create a new scene What I will do is that I will treat every canvas I can in my scene so I can treat No 2d. I can treat sprites. I can treat control nodes and as such I can treat this sliding panel using the same interface So I can call this show and hide methods on every one of them So this creates a horizontal interaction between these classes So I will create a a simple interface here And I will add this sliding panel to this interface And I will also have a sprite that I want to show as well And it will use this Sprite texture here is just to give you an example of how we can use polymorphism And let's say I will also have a button right So I will extend this button and put it here and I will make this sliding panel right here And here on the interface I will create a new script here And here we will have in the ready method We'll take every children for child in get children And we will check if this children inherits from the canvas item Canvas item So if they are a canvas item We will call the show method on them All right So this is how we can treat every single one of them as being of the same kind Even though the implementation of the show method here is different from the implementation of the show method of the sprite and the button So if I save this scene So just a small fix we have to call this on the current child So on the current object here in this iteration So with this fix if we go back here and test the scene Every single one of them will show itself But the slide panel instead of just popping on the screen it will slide on the screen So if you test again, you can see that it will slide So that's it for this series. I hope that you learned something new I hope that you took the most out of this series so you can improve your programming skills That said, let me know in the comments how you took the most out of this series What to learn what are the different concepts that you understood due to this approach And also if you're not subscribed to my channel, please do subscribe so you can get more of this content And don't forget to turn on the bell because I made a little research and I just figure out that YouTube is indeed not delivering my content to the subscribers So please turn on the bell if you want to get notified when I release a new video This will help other people get known to this kind of content So if you appreciate what I'm doing here and if you perceive the value that I'm trying to provide Let other people know that this kind of content is being produced on the internet, okay? And also you can support my work and what I'm doing here Just like Alfred Hynald-Baldes and Patrick Farner did by becoming one of my patrons Thank you so much Alfred and Patrick for your support. I appreciate that very much This is what keeping my work being produced here in the channel But that's it. I want to thank you so much for watching keep developing and until the next time