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

Public and Private

In this video we start thinking about hiding some information using Public and Private Access Control Modifiers. In ActionScript 3 there are a few types of Access control modified but we will focus for now on the two most common ones Public/Private and visit others in the future.

Video Transcript

00:00My application timeout now has a default time. I want to store that amount that seconds into memory
00:07So to do that, I'm going to create a new variable and I'm going to call it total seconds
00:13It's going to be a type of integer and as soon as our time all begins. I am going to set our total
00:19seconds to be that seconds, okay
00:23As we were working with our constructor we from this point on we want to tell every single variable that we create
00:30If we want the world to be able to see it or not and we're going to see right now what that means
00:35We're going to start with first the word private
00:37Now our constructor always will be public no matter what okay?
00:41It's really important because our constructor has to be public for our objects to be well
00:45It's just the rule
00:46But also just logically for our application to be able to build that out that object to be able to build that time
00:52And it needs to have access to the function that builds it out right because we're sending information to we're building that custom experience
01:00If our variable is private our app won't have access to it
01:05But if our variable was public, let's start public and let me put there just some well
01:11We're not going to put something in there because we're going to send it in our timeout
01:14So as soon as I call my town
01:15I'm going to send a 10 and I want to trace out the value that's in there and the way I could do
01:20That is I could just call my total seconds. I just created and
01:24The total seconds will be available to me because it's public
01:28We're not going to look into other differentiations at this point for now
01:32There's only public and there's only private and as we progress in our skill set gets more we feel more comfortable
01:38We're going to add a few other items
01:40So for now we have public and we have private now if I would type private what that would mean is that if I try
01:46To run my application again. It's going to tell me whoa careful here. You're not allowed to access this information
01:51All right a little error 11 78 so total seconds is not available anymore because it's private
01:58That's actually good because I don't really want users to be able to access directly my variables
02:03Because if I set a timeout and I'm doing all this logic inside
02:06I don't want someone coming and changing the amount of seconds
02:08Just whenever they feel like it because it could create bugs and it could break the experience
02:13You're going to see that a lot of times developers are going to work a little bit more than they you know
02:18You would think logically that they need to because they want to protect variables variables
02:22Arab is data right functions is actions and
02:26Variables we don't have any control over them. Well methods
02:29We do have control over them and what I'm calling methods is functions that belong to a specific object by knowing that
02:36What if we instead created a public function?
02:41that
02:43Would send us our get total
02:47seconds and
02:48It would send back an integer and I really would have to do in here is just set return total
02:54Seconds and the reason why I could do it in here is because private is to the world
02:59But while I'm inside of this class, I have access to everything in this class
03:03Okay, but what I'm saying private and the rest of the world can't see it anymore
03:07So by setting a get total seconds
03:09It enables me now to have the user a way to get that information without the user being capable of changing the information
03:17Because the user has no more access. They can't just go if this was public
03:21They could have just gone to our total seconds, right? They could just grab that total seconds
03:28And just decide right now that from now on it's going to be 20, right?
03:33We don't want the users to have access to that right? That's private at this point
03:37We only want to let them actually view the data. So to be able to let them view the data
03:41We just created that new get
03:44total seconds is
03:46Which is a function and it's not a variable anymore and then they could do whatever they want with the data that comes back
03:50Cuz they can't affect our time out from our timeout class. Let's see. We have a very little error
03:56So prior spelling error total second
04:02Type that in there total seconds if we run it one more time, we're gonna see that it's getting our total seconds
04:08So we've learned a lot of really critical things together. So because we know what variables are already we know what functions are
04:14all we're really added into the picture right now is that variables belong to the objects and
04:20functions belong to the objects as well and when they belong the front a variable will be called a
04:26Property and when a when a function is belongs to a class or to belongs an object
04:31It will be called it will be called a method

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!