02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Get Ready Set, Go!

In this video we cover the importance of classes as a means to protect data. We continue and learn about getters and setters. We talk more about the importance of hiding our data from external users.

Video Transcript

00:00The next step that I wanted to talk about and we could delete probably these traces.
00:03The next step I just wanted to compare and just talk a little bit more about the differences
00:07obviously between an object in our class as we're a little bit new to classes.
00:11So with an object, we could have created as many properties as we wanted, right,
00:15we could have created properties just as much as we could put them in a class.
00:18So for example, if we wanted to, we could have had a property of eyes and set that value to be 2,
00:23and a property of feet and set a property to be 4, right?
00:27No biggie, basically the same thing as we have an animal.
00:30So why are we actually putting this into a class?
00:33And there's quite a lot of reasons.
00:34One of the reasons, one of the main reasons is that now that we have it in a class,
00:39we could actually approach our animal and by default,
00:41we're going to get all this information automatically.
00:44Not only are we going to get this information automatically,
00:47if we try to input any value that's improper,
00:49such as if we try to put into our eyes value, a string instead of an integer,
00:55then when we run our application, it's going to generate an error
00:58letting us know that we're putting an incorrect value.
01:00So that's already an advantage.
01:03The second advantage, which connects to our main topic of this, of the hour of this hour,
01:08is inheritance, as if we built this animal class that has all these properties,
01:13and later on, we would need to create another class.
01:15But that class is dependent on all these different functionalities
01:18that the animal class has, we won't have to rewrite them.
01:22So before we dig into that realm, I want to show one more reason
01:27why it makes sense to work with classes.
01:30So currently, we have our eyes.
01:33Imagine that we created now, not an animal, but we actually created a cat.
01:38We created a new cat, and we published it, and we took it out to the world.
01:42So now we have a cat, but the only problem is that anyone could come in,
01:46take, go to our cat, and say to our cat that from now on, you have only,
01:50well, now you have four eyes.
01:52And obviously, that's not correct.
01:53And not only that's not correct, that might create a problem in our application.
01:58Now, Flash can't recognize that, right?
02:00Flash, there's nothing wrong with us putting four eyes for our cat.
02:05Now, the way to solve that is, and that connects back to our topic
02:08that we said that we're going to be talking about, about private and public.
02:12Now, a good strategy in object-oriented programming is to set all of your functions
02:16and properties to be private unless they're needed in the external world.
02:21So let's start with our private, with our variables.
02:24I'm going to set them to be private, private.
02:27And I want to show you a strategy, a trick of how we could get a way for our user
02:33to be able to change the values of our eyes or get our values of the eyes
02:38without needing to have direct access to our variable.
02:41To do that, what we could do is I'm just going to create two functions.
02:44One function is going to be a public function called set eyes,
02:48which is very, very similar to what we've done so far.
02:50So this property, and one, I'm just going to change the name of my variable
02:53to be underscore eyes, so it's going to be easier for me to differentiate them.
02:58And I could type in here eyes, or let's put here a different value.
03:01Let's just put here eye because it's going to be an integer value.
03:06So I'm going to return anything.
03:09And when we set the eyes, we could even double check and say,
03:12only if it reaches a certain match.
03:15We could put here an if if we wanted to, but we're not.
03:17We're just going to set our value eyes to be that value.
03:20So it could be any amount of eyes.
03:22And we could create another public function and call it get eyes.
03:30And get eyes is going to return an integer, and it's going to return our eyes.
03:36So far, so good.
03:37So now our application, if we wanted to actually reach and read
03:39or change the value of our cat, we would have to go explicitly
03:43and go to a function, set eyes, and then we could change the amount of eyes.
03:48Now, when we do that, our application could then trigger an error
03:51if there is a need to trigger an error, or it could just ignore it and not let it happen.
03:58Ignoring it could be by using an if statement.
04:00So if we wanted to, we could have said only if it's in a certain range,
04:03such as one or two, because animals, as far as we know, can't have more than two eyes.
04:08So we could say here, if I is smaller or equal to two, then this is valid.
04:17Now, probably if we're already looking at this, integer is probably not the right type, right?
04:21Because there could never be a negative amount of eyes.
04:24So maybe we should set it to be a you end.
04:26That way, we don't have to worry about negative values,
04:29because we know that the value that's going to be set in here is going to be positive.
04:32So I just changed a few things around to make it more accurate for us.
04:36We're not throwing any error, but if we try to set our eyes and they are,
04:40we just can't set four, and then we just try to call that cat get eyes, and we trace it out,
04:48we'll see that all that even though we tried to send a value,
04:50even though we tried to set it to be four, it did not fall through.
04:54But if we tried and send it a value that is in the right range, such as one,
04:58we'll see that the cat will now have only one eye,
05:01because it had an accent or because the dog caught him.
05:04All right, so we see how we can work with our get and set functions.
05:10And with that in mind, I want to show you just one more thing really, really easy.
05:14It's called getter and setters.
05:16The point of getters and setters are kind of to hide from the users
05:19that this is actually functions,
05:21because we don't really need the user to know that's a function,
05:24and we want to keep their life easier.
05:26Just as much as if you remember working with movie clips,
05:29where you had X and Y properties,
05:31those properties behind the scenes are actually doing things,
05:34they're moving our movie clip in the screen.
05:36Same deal here, we could use getters and setters,
05:39and I want to show you how to use it.
05:40It's very, very easy.
05:41So with our function, instead of calling it set eyes,
05:43I'm just going to put here a space,
05:45and you'll see how my set word has become blue,
05:48indicating that it's a reserved word.
05:50And we have your set eyes,
05:53and we could do the same thing for get function, get eyes.
05:56And that was that easy.
05:57Now that I created a getter and setter,
06:00my users don't have to even know that this is a function,
06:03because this function is going to be hidden behind the fact
06:05that it's supposedly a variable.
06:07So let's see how that works.
06:08Instead of typing set eyes, I'm just going to comment that out.
06:12I could type now cat.eyes directly,
06:16and I could say, okay, now the cat will have five eyes.
06:19And now when I'm tracing it out,
06:20I don't have to call a function anymore.
06:22I could call it as if it's a property, eyes.
06:25Now everything is going to work.
06:26Let's just fix whatever error we have here.
06:28Double click on it.
06:30So let's read the error, because I don't recognize the issue.
06:33Accessor types must match.
06:35All right, let's see.
06:36Public function, set eyes, get eyes.
06:41Oh, so our problem is actually we have your uint, uint,
06:45but then suddenly here we have an integer.
06:47So we have to make sure that our accessor types match.
06:52And that's really the error that's happening here.
06:54It's saying that these two lines need to represent the same thing.
06:58So we fixed that issue,
06:59and we could run our application one more time.
07:01And we could see their application is working just as we wanted it to work.
07:05Now one more note about this,
07:07if we don't create a setter and we only create a getter,
07:10that way we could create a read only variable for the user.
07:13And that read only variable, if we run our application
07:16and we try to set the value,
07:18it's going to tell us that, whoa, you can't set a value.
07:20This value is not setable.
07:22And same around if we only wanted the user to be able to set values,
07:25but we didn't want the user to be able to read what is the value.
07:28But in our case, we're happy with having a getter
07:31and a setter as well.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!