 This video was brought to you by my patrons. Thank you so much for your support. Hello there! We are back to the video schedules and I wanted to start a new series. So a series about the four pillars of object-oriented design or object-oriented programming. They are abstraction, encapsulation, inheritance or class relation and polymorphism. But before we dive into the content itself, let me explain to you how I want to take this series. I want to make something that I didn't solve in any other place, that is to make the programming language be as understandable as human language. So my take on that is to make the programming language an way to automate your thoughts. So I have here this sliding panel and I will program it. I will describe its behavior just like I would describe it for let's say a peer, for a friend. So if my friend asked me what a sliding panel does, I would answer it in a given way. And this is what we are doing when we are programming. We are automating these thoughts, our thoughts on let's say a designing panel. So programming languages are just a way to automate your thoughts. This is my take on that. Many times I will describe something in an unusual way that's not technical per se, but that will at least give you some insights about what is the intention of that technique, for instance. I already have a video on encapsulation, but it's more a video about a workaround that I use it encapsulation to work around a limitation of implementation. And this one will be more about the actual encapsulation technique or the encapsulation pillar of object oriented program. So without further ado, let's get started. But before we dive into the implementation of this sliding panel and see how I use it encapsulation and use it as an example to understand the encapsulation features of the object oriented programming, let's see how it is described. So how encapsulation is described. So I will switch to a book here. And in this book, it says it states that encapsulation is the ability of an object to hide parts of its state and behaviors from other objects, exposing only a limited interface to the rest of the program. To encapsulate something means to make it private. So if you know the script, you can think about this and see, okay, so the script doesn't have encapsulation. But let's continue. And thus making it accessible only from within the methods of this of its own class. So we don't actually have private features or the private keyword in the script. But to make it private is to make it accessible from only within the methods of its own class. And if you watch the video on encapsulation that I have previously, you can see that we have this feature enabled for us to make something accessible only through the methods of its own class. So I will go back to Google and let's see how we can apply this. So I will open the script here and turn on the distraction free mode. But before we dive into the actual implementation of this technique, allow me to elaborate a bit on encapsulation feature, because I want to make this parallel with a human language or with a human thought. In the clean code book, they have a usage for the encapsulation that I found amazing. They say that you can encapsulate the implementation of a method through the method name or the implementation of a verbal to the verbal name. So let's say we have a verbal that its implementation says that it will be the normal of a collision or something like that. So we can encapsulate this feature or this implementation in the verbal name. Let's call the verbal collision normal, right? Because this will say what this verbal is and what will be the data that it will care and what we will get from it. This is kind of like how we use words into human language as well. Let's think about it. When we say water, we are actually referring to a given a range of molecules of the world, right? But instead of saying, oh, I'm drinking a diox of hydrogen, we say I'm drinking water, right? So this is the thing that the clean code is trying to pass through this encapsulating implementation. You can use this feature not only as a private feature of a programming language, but you can use this encapsulation to provide an interface of any implementation of anything at all. You can use encapsulation to encapsulate the implementation of a class telling that this will be a sliding panel, for instance, and the implementation will be that it will be slide and it will be a panel. And you can think about that just by reading the class name. The same goes for the verbal name. So I have here a title and a title is a text. So the implementation itself, I can presume that it will point to a string that will be the title of the panel, for instance, if we go back here, it will be this title here, and then the description here that will be this verbal here. So this is something that you have to keep in mind that the concept of encapsulation doesn't actually just apply to, let's say, making something private to use the private keyword. It means to hide the implementation and provide an interface to whoever is reading or understanding what you are saying so that they can understand, oh, so if you are saying that, I think that this is the content that you are pointing to. So it's a very important concept to have even when you are speaking to someone. So you don't have to explain the details of what you are trying to say, but you provide a single word or a small phrase that will encapsulate all the thoughts that want to pass through this word or through this phrase. But now let's get started to the implementation of this into a programming language. So I will take rid of this left panel here for an instant. And let's try to understand what I'm doing here. So to me encapsulation is to make data or to make implementation an easier to digest piece of information. So I have here the title and the description properties of this sliding panel class. And the thing is that when you are actually trying to set the title and the description of this sliding panel, it will actually go to the one of these labels here, because labels is what display text in Godot, right? But instead of throwing this responsibility of knowing that this should be set to a label to an outside class, what I did here is to make this very digestible to make this easier to access to provide an interface to other classes to interact with these labels. So I made here the title and the description point to these labels. And when some other class tries to access these values, instead, it will access the text properties of these labels. So when some other class tries to change the content of the title, for instance, instead of having to access all of these, it just have to say, okay, set the title to this string. I don't have to care about what it will be. I don't have to care about the implementation. Just just make the title be that string for me. And if you see, I will turn on the left panel again. If we go here into the game, you can see that this is actually everything that this class has to know about the implementation of the sliding panel, it just have to take the sliding panel and say, okay, this, let's go back to the sliding panel. So it has the title, then the description, and it also has a sliding distance. So if you go back here, the game class just have to say, okay, set the description to this text and set the title to this text and just show yourself. So you can see that the interaction within these two classes is very, very simple. And this is what you're trying to achieve with encapsulation to make the implementation an easier to digest information to provide an interface to be interacted with this class. And if we go back to the sliding panel, you can see that actually when the game class is telling the sliding panel to change the text of the title label, it is actually going to this path into the text property and setting it to be a given text lower case. So this is everything that the game class will actually pass through until the actual content of the title is changed. But for this game class, it doesn't care about that. It doesn't care what will the sliding panel do with the piece of information that it will give to it. You just need to know that, okay, set the description to that, set the title to that, I don't care about how you will achieve that, just do it for me. This is everything that this class should know. And this is what the encapsulation does. It will hide the implementation details to make these interactions as seamless as possible. Now, another thing that I want to show to you, I will turn off this distraction-free mode, and I will open the Moonsheeser scene here. And something that I want to show to you is look at the scene tree here. We have this sliding panel, right? But you can see that we have only the sliding panel. There is no child, there is no children on this node here. But if we go back to the sliding panel, it's way more complex than it seems, right? But here we have only the sliding panel. This is because when you save a scene as a branch, actually what you are making is encapsulating it. You can see that if I go here into this Moonsheeser, that is this game script, when I try to access the panel.description, you can see that we have the description here. We have the title as well, the timer, the twin, etc. But if we try to say that the panel will actually be the achievement slash sliding panel, and you can see that we have way more children to go. So it is way more complex than it seems. But it doesn't have to be that complex. And if I go here and make it Editable Children, you can see that we have access to all the children. But the idea of making a branch an independent scene, so to use this save as branch feature, is to encapsulate this branching of the scene tree into a single scene, into a scene that will be independent by itself. And therefore other scenes or other nodes doesn't have to have access to this children, to the children of this branch. So this is another feature that you can use to make your scenes encapsulated. So to make your nodes being encapsulated, not only your code. Because remember, the idea of encapsulation is to take a complex set of data, and make it easy to digest. You can think about the capsules. So a capsule is a complex set of machines that you don't have to care about what is the content of the capsule itself. You just have to know what is the capsule and what it is meant to be used for. And you'll just take it. And instead of having to take a bit of each medicine that is inside this capsule, you just have to digest this capsule at once. The same thing goes for node trees. The same thing goes for classes, for methods, for everything. You take information, you take complex data, and you encapsulate into easy to digest piece of information. So this is my take on encapsulation. I want to show with that, remember that I want to abstract this concept so we can apply it to other fields, not only when you are programming a class, not only when you are writing a code, but to take this concept and apply it into other fields. When you are communicating to someone, when you are making a scene, when you are trying to explain something, when you are making a food, maybe you can encapsulate the ingredients into a more digestible piece of food or meal. Take this concept and apply it to other fields as well. This is my take on that. Also, before my feral, I want to welcome Mike King, my newest patron. So if you like my content, you can do as Mike King did and support me on Patreon as well. I will redesign the rewards and I will add some goals. This year I want to have some goals with Patreon. I want to make some amazing things with that. So join me, join my big club, and you can also participate into the pools that we have on Discord. I will have more pools exclusive to patrons as well so they can have some decision power with my content. And well, just join me and help me make amazing content. I hope to continue making the content, subscribe as well so you can get more on this content and you can help me grow the channel. That's it. Thank you so much for watching, keep developing and see you next time.