 Welcome to Intro to Java with an emphasis on AP Computer Science A with Tokyo EdTech. That is me. Today's topic is using objects part 2. So we're going to revisit objects, we're going to revisit classes, and we're going to learn a little bit more about proper object oriented programming principles. Let's take a look at our list. So as I mentioned, we're going to review what we did before. We're going to talk about the idea of encapsulation, which is a very important principle in object oriented programming. And to carry out encapsulation, we need private attributes and public methods. In this particular video, we're going to only look at setters and getters, their particular type of public method. And, you know, later we'll look at other types of public methods as well. And then talk a little bit about validation and why we might want to use this way of doing things rather than the way that we learned earlier in this particular course or video series. So let's go ahead and get started. So just as a reminder, I have a simple person class, and this goes back in quite a few units. And so we said a person is an object that has a name, and it's a string, such to be a capital S, not a small S, and that person should have an age, and that age is in it. That's the only thing we've really done with objects up to this point. We talked a little bit about methods, static methods. Now we're going to be doing non static methods with our objects. So over here, I can go ahead and create a person object. So I think the example I used earlier was Ironman and equals person. And so Ironman is a person. So then I can then say Ironman dot Ironman, Ironman, Ironman dot name equals, we'll say Tony, and we'll say Ironman dot age equals 53. And then I can go ahead and then print those system out that print ln. So Ironman dot name system that out that print ln Ironman dot age. So again, we're seeing dot notation. So we've got object dot attributes. So we can compile this. And kind of find symbol because I forgot to compile this one. file that one first, then compile this. Kind of find symbol. Oh, I forgot new. And compile that again. There we go. And so if I run this, you can see we've got Tony 53. So we've created our Ironman object. Ironman is an instance of the person class. We've assigned values to two of the attributes. I'm going to put a space there so we can more visual break up there. So we've got a string Tony is now the name and the age is 53. I don't know where I got that from probably the internet somewhere. And then here we actually printed out those values. This works. However, we're violating the idea of encapsulation, which basically states that these attributes over here should not be accessible directly outside the class. So what that means I need to change this to private. And I change this to private private as well. I'm going to save it. I'm going to compile it. Now I'm going to come back here and try and compile this. And now we get some errors. Okay, so you'll see here it says line eight, Ironman dot name equals Tony says name has private access in person. And the point to tell you that here where this problem is, you see the same thing age has private access, you know, all these different lines, we get these errors. Okay, so because this is private, we cannot access it from another class. So I'm going to go ahead and comment that out and show you how to actually do this properly. So as I mentioned, we have our private attributes and those actually go over here. So we've got string name and int age. So what we need to do is we need to create two types of public methods. One is called a setter, which is also referred to as a new tator method. And getters, which are called sometimes referred to as access or methods, I do like consistency and methods. So setters are just what they sound like, they're going to set the values. So the key thing here is the attributes are private, but the setters are going to be public. So public, void, set name, notice this pattern, string name, we say this dot name equals name. And what that means. So then, sorry, I should say over here. So then I would say iron man dot set name, Tony, if I compile this, go over here, compile that. If I run it, we're not going to see anything because we just set it. But what happens is this value Tony, the string is sent to this non static method. And so name equals Tony, you see this dot name, probably wondering, well, where does this come from? Well, this is actually iron man. So this set its name to Tony. We do have another option with this. The reason I had to use this is because name and name are the same way. It's a conflict. We could do something like, you'll see like my name. You could do, you know, name equals my name. Sometimes you'll see, sometimes you'll see n. That's a common one too. I don't personally don't like it, but you will see something like this. So name equals n. I strongly encourage my students to be aware that other people do it right it that way. But I feel that this consistency is really, really important. So you have name here, you have set capital name, string name, this dot name equals name. It's a very simple pattern. We can do the same thing with the age set age, age into age, this dot age equals age. Very consistent. We compile it. And then we go ahead and do iron man dot sets age and 53. Let's go ahead and compile it and run it. Yeah, we see nothing because we haven't printed anything yet. So that's where our getters come in. Getters allow us to access that information. So go ahead and do my getters. So getters, their job is to return the value. Since we're returning a value, we have to be aware of the return type. So in the case of name, it's going to be a string. So we do get name. We're not sending anything. Then we just do return. We can do return name because there's no conflict at this point. I again, I like to keep it consistent and do return this dot name. And especially as beginners, I think you'll find it helpful. And then we also have public public int get age, and the type the return type needs to match. So this is an int. So this is an int. It's just it's a simple pattern. This dot age. But again, you can do return age as there's no conflict. Compile it. And over here, I can do system out print l n iron man dot get age or get name, extra parentheses there. And copy and paste that iron man dot get age. Compile it. We run it. And we see Tony 53. So you can see here, it's slightly different. So instead of object dot attributes, we need to do object dot method. So this is our setter method, our mutator changes values. And we're sending a value in each case. And this is our accessor method, which allows us to get the values from that object. So that's basically how you do setters and getters. And you're probably wondering, you know, why, why would I bother with this? Doesn't really make, you know, I was doing just fine before and you were. But this is an important principle in proper object oriented program. If you are a an AP computer science a student, you must have private attributes and public setters and getters. If you don't, they do take off points on the AP exam. They're very strict about that. So you got to get into this habit from this point forward. And but one of the reasons that you might see this is for validation. Let me give you a quick example. So what if I did something like this iron man dot set age, and just, you know, users have a tendency to enter weird values or wrong values or maybe this 300, whatever. If I do this, then I go ahead and let's say the data, you know, somehow the data gets corrupted. There's a problem. It will allow me to enter a negative age, a negative value for age. So what I can do over here in my center is I could do something like this. I could say, well, if age is greater than or equal to zero, and age is less than I'll say 125. And I chose 125 because I think the oldest living person is like 122 123 like confirmed. So I added a couple years just to just to do that. So if the age is in that correct range, I go ahead and do it else I would do system that out that print L and sorry, ages must be between zero and 125. So what this does is this present prevents, I should say, bad data from getting into a system and then causing problems later. So validation. So we're confirming that the numbers are in a certain range. Okay, so let's go ahead and test that. So compile that and run it. You'll see here, sorry, ages must be between zero and 125. And then so that value was never changed. Now, if you learn more about programming, you would do something called throwing an exception, and we tell we deal with errors and things like that. But for now, the well, the AP doesn't cover that. So I don't really mention my course. But for now, we can just print, hey, sorry, you got to do that again, that was a mistake. So this really saves us, it saves incorrect, incorrectly input data from getting into our system. And again, causing props, like let's say we had to calculate, you know, the difference in ages, this is minus 53, you know, you just you could just see where things would get messed up. Now, again, in this case, it's not a big deal with some age. But if you were writing a program to control some valves in a in a factory or something like that, and you're supposed to set the valve to a value between 20 and 80. But you accidentally try to set it to 100 or 110. And that causes a physical malfunction because the physical object that the controller is running can't go that far. So if we had direct access to these attributes or to these attributes, we could put in any value that we wanted and start causing problems. However, in this way, by using centers and getters, we can make sure that the the range is put that in there that the range is correct. And it's not going to cause problems. We can also say so we might also want to say let's say we had private private int ss number social security numbers in the United States. So let's say zero. So we might need to have a social security number in the system. But I don't want it because it's super important private information. I don't want it to be accessed from other classes. I only want to be able to access that information in here. So in that case, I just would not have a getter. I just I would not do public int get so scary number. I would just leave that out. So the information can still be set. But I can't access it outside of those methods. That's that's kind of the reasoning behind using private attributes and public methods. Again, it's more along the software engineering side of things. So in the beginning, of course, you don't really run into it too much. But it is something to be aware of. And as I mentioned, the AP does require that you use private attributes and public mutator and accessor methods. So I think that's it. I think we covered everything for this particular video. We reviewed making objects. We talked about encapsulation. That's the idea that attributes are private. And we use public accessor and mutator methods. We saw that one reason to do this is for validation to make sure that our data falls within acceptable parameters. It doesn't cause problems later in our code. So thanks for watching and keep on coding.