Access Modifiers
In our last video we created our first object in this one we explore the idea of hiding some of the information in the object. the idea behind it is if we have many objects that can interact with each other we really want to control what can be accessed and what can not. our current problem is we have a "cat" object that has many functions/methods but we really don't want others to have a way to call these functions such as internal operation functions. we talk a bit about the impotence of hiding information that others shouldn't know about.
Video Transcript
00:00So basically, one of the stuff that we have here, everything is great, everything is related to our
00:04specific cat. But what would happen if someone were trying to access directly something we don't
00:09want them to access? So in our scenario, Jerry, the cat is a cat that whenever you ask it to talk,
00:15it talks, whenever you ask it to eat, it eats and so on and so forth. But we don't want anyone
00:20actually creating the body more than once. This is something that is really personal to the cat,
00:25right? We don't want anyone to keep build more than one head. We don't want anyone to define the
00:30color of the cat, you know, we don't want people to grab cats in the street and color their fur to
00:35a different color, right? That's just not, it's not a publicly available method of a cat. And
00:41we're going to get back to that idea. So basically, we need to find a way how to actually hide some
00:46of this stuff, and not let other people other things that are sad, because now that we're
00:50working with objects, other objects could then interact with each other. So if I have your
00:55cat, that's Jerry, and imagine that there's another cat out there and the other cat comes
00:59to Jerry and calls the eat function, obviously, all this stuff you're doing yourself. But
01:06we don't want it to have access to a few things. So that's our first problem right now.
01:11Our problem is, okay, we're great. We now have all the cat related things in one place. Now
01:17it's organized. Now we have five, six different types of things are all organized into categories.
01:22We're not worried about things overlapping with each other when they don't need to.
01:26But now we have a new issue. Now when we need to interact with the cat, I'm overwhelmed by the
01:31amount of functions. Okay, so now that our application has continued and grown, now our
01:35cat has 500 functions. And that's also too much for me to deal with. And actually, some of these
01:42functions, if I called and would mess things up, such as creating the head again, it should only
01:48be called once, right? It's something internal that shouldn't be really available for anyone
01:52else only personally, internally, only the cat himself should have access to it. All right,
01:56so that brings us up to the next topic that we're not going to cover in depth at this stage of our
02:01training. But it's called access modifiers. And the whole idea of access modifiers is really to
02:08basically control who can see functions and variables, or in the object oriented naming
02:15methods and properties, same thing, right? So there are four different types in flash,
02:21and I'm sure there's others and other languages, there's a public one, which basically says anyone
02:25could touch it, anyone could interact with it, there's no, no limitations involved. There's the
02:30private one, private basically says only that specific class has access to it and no one else,
02:35but that specific, well, I threw there a new word, which is class, so forget about class,
02:40only that specific object only that specific element only that specific cat has access to
02:46those functions. protected is something that we're going to look at, hopefully, if we have time later
02:51on today, but protected is in a scenario where there's inheritance, and we're going to look into
02:57that in a later on a later stage, I just wanted you to know that these stuff exists.
03:01And internal is another one that flash has a basically says that there's a function here,
03:07but it's not really accessible to you, we're not going to actually show you the actual
03:11code of that function, it's internal to that, to the proprietary Adobe things at this stage,
03:21you never know if flash will be sold or something will happen to it at some stage.
03:25But and by the way, this is also true with a lot of other languages. So it's not really
03:29specifically flash related, there's access modifiers basically enable us to control who
03:34could see the specific functions that we have. So let's, let's really focus down into these
03:39public and private because the other ones are not as relevant. And when you actually are more
03:44familiar with things such as object oriented, well, things such as inheritance, then the rest will
03:50will really make more sense. So in the scenario of our public and our private, if we go back to our
03:56function, and we just hit all of them a bit kind of made them faded out to kind of represent that
04:01they're hidden and they're internal and they're private, although by default, anything you create
04:05inside of your class to be when you when you build classes is automatically going to be public.
04:11And the way for you to set it to be private is to go to that specific function, basically type
04:16before the function and type the words private space and then the name of the function, you know,
04:22private space function, then the name of the function. So let's see in our scenario,
04:26we definitely want to eat to be public, we want the talk to be public, we want the scratch,
04:31we want to fly, we want the whatever this one was, we want the drive, which I probably shouldn't
04:38have made it completely opaque, but can't change this stage. And let's say that all the rest you
04:43really don't want anyone touching, okay, all these rest are stuff that we wanted to be completely
04:47private. And obviously, my drawing is getting uglier by the moment. Alright, so so far,
04:53great, we, we we hit exactly the jackpot exactly what we wanted. Now our function now our specific
05:00cat Jerry only has functions that are available to the world when we want them to be available
05:06to the world and all the rest are hidden from the world. And by the way, the name of
05:12the interface or the name of the things that are available to the world is API.
05:16So a lot of times you're going to see that in programming documents, a lot of times you're
05:20going to see that in in different in different dialects that are related to programming and
05:25object oriented programming. Really, when people say API, what they actually mean is the publicly
05:30available functions of a given class or a given or given object. It's the API, it's the face fronting
05:37things of that object. So in our scenario, our our API is all these functions that are
05:42available to anyone to interact with.
