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

Why Use Private Access Modifiers

One of our students asked why should we use Access Modifiers in our classes? in this video we explore this question and answer the impotence of protecting data from myself and not only from others. for more information please watch video.

Video Transcript

00:00So let me let's uh go back to the questions here. So there's a couple of questions
00:03Are these act as modifiers more important than working with others?
00:08On this are more important when working with others in the same project
00:10Who are you protecting these functions from if not other programs who don't know the code? That's actually a really good question
00:18It's actually uh, sometimes it is for other people but for now, let's forget about other people
00:22Let's focus on you and why would it be beneficial for you to always put private on things that you don't want to be?
00:29openly available to the world now
00:32The whole idea of why we worked and moved into object oriented programming was really because we had this need to
00:39Organize and we wanted to make sure that we don't have to think about things we don't have to think about
00:44So when I created my cat class, it took me 30 different functions to create them
00:49But after the cat was created
00:52I really don't care how it was created
00:54All I really care about is the actual functions that I need to interact with it
00:59Because now after the cat is created I could now create um, I can now
01:03Create a cat and put it into another type of class in my application
01:07So by me putting private and all this stuff that I know have no relevance to the api
01:13Have no relevance to someone that's outside of that class
01:16I'm protecting myself as the developer
01:19From calling by mistake functions that I didn't really want to be called
01:23And not only that in modern um
01:26And modern editors i'm also only going to get um
01:29Hinting i'm going to get hinting for all those public things, but the private stuff are going to be completely invisible
01:35now the powerful thing about something like that is
01:38I have to I don't have to think about as many things
01:41So when i'm looking at a cat class, I don't have to search in a hundred different functions
01:46To find out which one I want to use even more than that
01:49Imagine that you have different properties such as let's say a variable that has the name of the cat
01:55If you set it to be public
01:58Or you don't set it at all someone could by mistake including yourself by mistake
02:02You could override the name of the cat without meaning to override the name of the cat
02:07Now if you would put there a if you put a modifier in access modifier and say okay
02:11This is a private. I don't want anyone ever to touch this besides something. That's internal inside of this class
02:17Then then it's much easier for you than to to control
02:21Issues. Um, I just one more example before I go back into the chat. Let's say for example, you have what's on age for the cat
02:28And the age grows once a year, but only once a year now it has a function that says, you know
02:34Upgrade my age
02:36Right, and there's another function that says what's my age?
02:39now
02:40You want other people to be able to know the age of your cat?
02:43But you definitely don't want anyone having access or a way to change the age of the cat
02:47Besides the internal mechanisms of that specific app, right?
02:52Or if you have a watch and the watch outputs the the time of the day and outputs the date and outputs all these great stuff
02:58But you don't want other people modifying or messing up with your with your watch if it's an active watch
03:03Including not yourself. You don't want by mistake to make changes that you didn't really mean for them to happen. So the idea behind
03:10Always putting everything as as as private and making it public based on your need is a really good way of working because then
03:18You're making sure that you're not exposing more than you need to expose
03:22And it makes it easier for you then to control the quality of your code and it makes it easier to make sure
03:26That your code is not talking to other things when it doesn't really need to okay. Does that answer that question?
03:34Exactly. Okay, so so so it's actually a really a really good way of of trying to protect your
03:41your
03:44Intellectual thoughts so kind of like when you're done with a specific stream of thought which is basically a class
03:50You then hide everything in there
03:52And you only expose the stuff you need now that when you when we look at for example this cat again
03:57We don't even care how cats are created. All we care about is the cat
04:01So now we have a cat that could eat drink poop and so on and so forth
04:04And we don't have to know how does it poop?
04:07We just know that if we call the poop function the cat will poop
04:12And and now when we put this cat into a zoo
04:15And we have all these different animals in the zoo and they're all interacting with each other
04:19And then we could approach the zoo itself and say zoo is open now or feed all the animals or whatever that big
04:27Functions are related to a specific zoo
04:30We don't have to worry about the zoo
04:33The guy that's going to feed all the animals in the zoo and he's going to feed all the different animals
04:38Um doesn't need to know how the animal choose the food or it doesn't need to know how whatever all they need to do
04:42Is that they need to give them the food and the animal itself knows how to take care of the rest of
04:47Its business if we were working in a procedural way of coding
04:51We would not only would we have to know exactly how every animal eats in every given time
04:57We'd have to know about everything always
04:59And the whole idea of object-oriented programming is kind of telling you whatever you don't need to know about
05:04Don't know about so the only time you would have the access to all these internal mechanisms of a specific object is only after your
05:12Inside of that class and you're you know
05:14Intertwining with its insides and then when you're out of that class
05:17It's as if it's a blue box
05:19You have no idea how it works all you know is that it does a few specific things
05:23And it also helps when you get down to actually decode on debugging and so on and so forth

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!