 Welcome everybody! This is a very brief video, I think, on the topic of encapsulation in computer programming. So this doesn't necessarily only apply to AP Computer Science, but this is what I'm making this video for, is for my students. So basically, from the Wikipedia, here's the definition of encapsulation. In object-oriented programming, and if you're programming in Java, you're definitely programming object-oriented style, encapsulation refers to the bundling of data with the methods that operate on that data or the restricting of direct access to some of an object's components. Pretty cryptic. Basically, what that means is you have an object and you don't know how it works inside. You just push a button and it does something. So there's a button, there's a switch, there's a dial, whatever that case is, you push it and it does something inside. But you do know what that button is going to do. So you may not know how a car works, you may not know how an engine works, but you do know that if you turn the key to your car, your car is going to start ideally. So let's take a look at an example here. So what I've done is I've created a tic-tac-toe class, a very simple one, and I've created two different boards. I've created board A and board B. So I've got a board A class and a board B class. And so what I've done is each of these boards has three methods. One is add move, one is clear, and one is two string so we can print out what the board is going to look like. And then what I've done is I've kind of simulated a tic-tac-toe game here where we add certain moves. Now, in this tic-tac-toe game, you are basically interacting with it. So the top left corner is position one, the top middle is two, top right is three, kind of like a old school, you know, dival pad or a phone number, dial or thingy. And so for example, so you add your move. So I want to say add X to 10. Now 10 doesn't exist. So we should get some kind of message telling us that that is an illegal move. And then I just simulated a few different moves and then clearing the board. And each time I print out the board to make sure that it's doing what we expected to do. Okay, so here's the result with board A. So I'm going to run it and then I'm going to scroll, pull this up a little bit and scroll up. Okay, let me clear that and run it again so we don't see all the old stuff. Okay, so you can see it says board A is ready and it prints out an empty board that shows us that it's set up the way we want it to be. We try to put player X into position 10 and it tells us that's an illegal move because the position must be between one and nine. Then we put player X again in position five and that puts it in the center square, of course. Then O takes position one. Then X tries to take position one but it gets illegal move because the space is already occupied. Then X says, okay, I'll take position two instead. O takes position seven and X takes position eight. And then finally, we have a winner so we will go ahead and clear the board. So this board object, this board class, has three methods. Again, add move, clear and to string. We have no idea how it's programmed inside and we don't know what data structures it's using. We don't know what type of code is there. We just know that if we do add move, which takes a string and an integer, it will add the move for us if it's within those ranges. We know that if we send the, if we call the clear method, it will clear the board and empty the board. So that's all we know about. Those are the publicly accessible traits or publicly accessible methods for the board A. So I'm going to go ahead and comment that out and I'm going to try the same thing with board B. And then again, I'm going to run it. Oops, actually let me clear this. Okay, I'm going to clear it. And okay, pull that up again and scroll up. So now it says board B is ready. And so same thing. Player X is trying to move position 10. Illegal move. Then player X to position five. Player O to position one. Again, player X to position one. That is also an illegal move. The space is already occupied. And then player X goes to position two. Player O to position seven. And player X to position eight. And we clear the board. So notice, if you recall, there's absolutely no difference between how the board A class functions and how the board B class functions. They have identical public methods. Or I should say publicly accessible methods. Add move, clear and two string. They do the exact same thing. Now let's take a look at the code. Okay, so board A is very simplistic code-wise. I'm just using a bunch of strings. And I'm using C here because I'm using in conceptually I'm saying it's a cell, like in a spreadsheet. So I'm just using C1, cell 1, cell 2, cell 3, etc. etc. The constructor is called. It just prints out board A is ready. And then here's our add move method. And this is very, very simplistic. It doesn't use any loops. It uses a bunch of if and else if statements to check if the position is, first we check if the position is valid, greater than or equal to one, less than or equal to nine. If it is, then we check to see if the spot is empty. If it is, we update it with the new player. And then else if, else if, else of if. And then if we get down to the bottom here, it must have been an illegal move because the space was already occupied. Or in the case where it was outside of the range, it says the position must be between one and nine. And then down here, when we clear it, we just set C1 back to space, C2 back to space, C3 back to space, etc. etc. And then two string, we just, we create the output and then we assemble it line by line. And then we return it. So that's the two string method. Now, again, private, I can't access C1, C2, etc. directly. I don't even know it exists in this case. It's just, all I know is that I've got a constructor. I've got a public method called add move that takes a string and integer. I've got a publicly accessible method called clear takes no arguments. And I've got a publicly accessible method called two string that returns a string. When we use the system out print or system out print line. Now, board B is a bit more come, a bit more, I guess, advanced code. I'm using a, an array here for my cells. And I've got zero. I use the blank space here. This is zero. And this is one through, and this is one through nine out this way. I called a board B and it says board B is ready. Now, this is essentially the same code structure. But in this case, I'm using the array to access it. So I don't need if cell one equals if cell two equals. I just use if cells position equals. And so again, structurally, it's the same. But you can see what's nice about this. If I wanted to make a larger board that was say four by four, all I would have to do really is change this to, what I guess it would be 16 or 17, whatever that would be. So I could make almost an arbitrary size board, depending on how I want to do it. And then clearing the board also is I'm using a loop and I just set each position to a space. And from one to nine, because that says less than ten, and then the two string method, I use cells one, cells two, cells three. But what's the principle of encapsulation is I don't know anything about the code of this object. So all I know is the publicly accessible methods and their signature. In this case, add move and string and int, clear and no arguments, and same thing for two string, but it returns a string. So board A and board B, their public methods have the same signature, same return types, but the implementation is different. And that's encapsulation. So we don't know what goes on inside the class. All we know is how we can interact with it. And that is one of the bedrock principles of object-oriented programming. Okay, that's it. Enjoy.