 Welcome to intro to AP computer science for new AP teachers. This topic is using objects part 2 So in this video, we'll be taking a look at reviewing class structure reviewing instantiation public versus private. This is where we switch from using public attributes to private attributes and if they're private means we can't access them outside of that class So we need to use mutator methods which are called setters and accessor methods which are called getters And then of course the perennial questions students will give you as well Why and talk about some reasons why you might want to do it this way rather than just doing what is kind of the easier way Which is making everything public and then a little bit of an introductory exercise similar to some of the stuff that we've done previously So first I would just review the class structure, you know class class name listed attributes Basically, it's you know public you know data type name equals and the default value again They should be used to this hopefully you've been doing this all along in the various units and just kind of reinforcing this idea as we go along And then review instantiation. So where we've you know, we've created the class. We've compiled it We create some objects. So it's class object name equals new class. And again, those of you familiar This is obviously we calls the constructor Talk about that in another unit. Again, I tend to leave some things out when I chase this course until they actually need it So I'm really trying to scaffold this a lot better than I have in the past especially students new to coding job is very complex And I just I'm just trying to get here's what you need right now to learn this particular concept without too much extraneous information And then we were setting values just directly. So object name dot attribute equals value Now we can do that because they were declared as public But now we're going to declare them as private. So I'm not gonna be able to do that. So public versus private It is against object oriented programming principles and AP rules to use public attributes on the AP will take off points for Using public attributes. So that's one thing they definitely look for So make sure that students switch from now on never use public for attributes. They should all be private And then when we access them we instead need to use the public methods Which are called setters and getters when we get to the next couple slides So setters these are also referred to as mutator methods as mutate needs to change and Consequently setters allow us to change the value of an attribute. So there are two kind of standard ways of doing that You'll see public void set attribute type for example new attribute um, and then you say attribute equals new attribute. So This would be like set name new name Name equals new name And this is one way of doing it. Uh, again, it doesn't have to be You know new attribute. I just I do this because it's very very clear. You'll see a lot of times Let's for example, it's set attributes like set name. You'll see type n And then attribute equals our name equals n. I personally don't like that I really emphasize to the students that uh variable names should be extremely clear Uh, I shouldn't have to guess the tiniest bit. What a variable it is. So what's n? Is that a number? Is it, you know, it's just keeps things very very clear Uh, the personal the method that I prefer to do is public void set attribute type attribute This dot attribute equals attribute. So set name type name this dot name equals name One of the reasons I prefer this uh is a it's just very clear But b is for my students coming from python and python will use self and self and this are basically equivalent So it's a little bit easier for them to adjust to this style And so that's how we set values now if we actually want to access them accessor methods get those values out and do something with them Uh, we use a getter and they allow us to access the value of an attribute. So it's public type Get attribute return attributes or public type get attribute return this dot attribute So for example public string get name return name public Uh string get name return this dot name So again, I'm really really picky about naming and I just make sure the students do everything Exactly consistently over and over again. So there's no ambiguity Now students will probably ask you why bother, um by nature students tend to be a little bit lazy And this seems like a lot of extra work for no real benefit And so just kind of tell them some values should remain hidden For security reasons with getters in the case of getters or some things we don't want Someone outside of that class to know So for example, maybe like you could think of like a social security number or some sort of identification Uh that we don't want to share with another program. So the idea is to keep that data secure The other reason for setters is that it allows for validation. The example I give my e-book is let's say You know, we somehow incorrect information gets sent. So what's your age and somebody enters negative 53? And so in our center, we could say well, if the age is greater than or equal to zero You know this age equals age otherwise print you know system out print line error So basically make sure that we avoid An incorrect range of values and that keeps our data, you know, secure and consistent So the introductory exercise is similar to what we've done in the past So as the students to choose a famous person, uh, or it could be something in class Doesn't really matter or just you know, they could choose whomever Describe that person and then google that person And so determine which info can be known just by looking. So for example hair color eye color height So that that would be what we consider public information Anybody can get that information just by looking Um, that's kind of the analogy I use otherwise it'd be something private. So for example blood type Um birthday, you know, that's right. You can't know somebody's birthday unless you ask them You wouldn't know somebody's blood type unless you ask them I use blood type because I live in Asia and Japan specifically and that's kind of it's a big thing here It's kind of like zodiac signs back in the u.s. So, um Yeah, so the public versus private So there's some information that we know just by looking but some information that we have to ask a person So there we are we've reviewed the class structure. We reviewed instantiation talked about the difference between public versus private We talked about the two types of methods the setters the getters And talks about you know, some reasons why the students would want to use this and beyond just the fact that the ap requires it to be used And then had a hopefully a helpful introductory exercise to illustrate the concept. So thanks for watching. Take care