Public and Private
Private and Public. should we separate between them? While we are talking about it lets really get what public and what private are using some really simple ideas from the real world.
Video Transcript
00:00Private public. Now, we're going to definitely look at this a lot. We're going to look at it a lot.
00:05We said that there are API applications. There's things we want others to know about. There's things that we don't.
00:10Just as we have in our constructor a public method, what that public method did, it said this has access to the world.
00:18Why did our constructor need public access to the world?
00:21Because our constructor, which is the name of the method that has the exact same name as the class,
00:26the reason why it's called a constructor, because inside of it we put all the instructions.
00:30When we create a new object, when we create a new blueprint, the first thing that it does, it runs through the lines that are inside of this function,
00:37which we call a constructor, and it does them. So this function is called no matter what when you create a new main.
00:45And we're going to look at how to create a new main, but it's the exact same way as we created a new object or a new anything else.
00:51What you get out of this is that we have public, and then we might have things that we don't want to share with the world.
00:56There's things that we're doing that we want to keep private to ourselves, such as inner functions and things that are not related, such as if we have a cat.
01:04Now cats, all cats breathe. Let me just put it here in the chat.
01:08Because deliberately I'm putting really, really silly and really like, and everyone does that,
01:12but really the point is just to make it really, really crystal clear.
01:17End of the day, anything in programming, humans created it, they were meant for humans, even if they were geeky people.
01:24The idea behind object-oriented people was really to find a way to be able to communicate in programs the way we communicate in the world.
01:33Now it's as close as we can get in object-oriented programming, and really that's the thought behind it.
01:40So anytime you don't understand something, always try to move back into the real world and try to create a real world scenario and think,
01:47how would you solve that in the real world?
01:49And you'll find out a lot of times that this is very, very similar.
01:52Alright, so let's just look at this from a real, real case, real world scenario.
01:57Now, I like cats, right? I have two cats.
02:01Now, do I ever need to, and I'm asking you guys, do I ever need to have access to their heart?
02:11Good answer, Danny. I hope not, exactly. And if I did, then that would be a little bit creepy.
02:22And that's where you want to take it. What are private properties, functions, whatever?
02:30It's things that, okay, now that we have that object, right, we talked about that circle.
02:35Now that we have that thing, it's none of your business how it works.
02:39Not only it's none of your business, we don't want you to touch a cat's heart.
02:42That's messing with the wrong things, right? You could pet it, you could feed it, you could...
02:48Basically, that covers it, right? You could feed it, pet it, and let it sleep.
02:53That's basically all operations of a cat, I think.
02:56Maybe you could buy it a toy once in a while and give it water and clean its sandbox,
03:01but it has nothing to do with the cat. That's already another object, right?
03:04If we're looking at it in an object-oriented programming kind of way.
03:08So really, the only times you're interacting with a cat is when you're feeding it,
03:11when you're, you know, grooming it, and when you're feeding...
03:15Well, I said it again, but there's like four or five operations that you do with a cat.
03:19The cat's API, to me, are four or five things.
03:22Cats have hundreds, if not millions, of different operations.
03:26I'm not aware of them. They don't want me to be aware of them.
03:29Whoever created them, God, if you believe in God, or science, if you believe in science,
03:34or gaudy science, if you believe in both, doesn't really matter.
03:38It's none of our business. Maybe doctors or scientists do know it,
03:41but then they usually open them up to see stuff.
03:44And that's really the point.
03:46When we're creating a class, we're creating an object, creating something.
03:49We want to think about two things.
03:51One is, what are the stuff that we need to know about when we're building it?
03:54And when we're done building it, now it's on the table.
03:56Now it's a, mmm, now it's a mane, or now it's a cat, or now it's a kangaroo,
04:00or now it's whatever it is. It doesn't really matter.
04:02What does the world, including myself, need to know about it to be able to interact with it?
04:06So when I'm building the mane, really, I don't want anything to be public, right?
04:11Because the only thing that interacts with it, with my mane, in this scenario,
04:15is the FLA that created it. It calls this function, and all the rest is going to be private.
04:20Why? Because no one should ever call our mane, again,
04:23unless there's something specific that we thought about that we needed it to be interacted with.
04:28So for that, everything else that we put into our mane will probably be public besides our mane.
04:34Does everyone follow me on that one so far?
04:47So we've seen a little bit about private and public.
04:50Let's say our constant. Let's look at our constant.
04:53We could add to that constant, and in the beginning of it, just say private.
04:57Now on, what does that mean?
04:59That means that no one could call these users only inside of the class itself it could be called.
05:06Now, what is that good for?
05:08And you might say, well, but when I create a cat, I am the cat. I should have access to it.
05:12And the answer is no.
05:14And it kind of connects the exact same thing.
05:17We talked about cats. Let's talk about ourselves now.
05:21Let me ask Carol.
05:24Again, sorry for asking a really, really silly question, but it's really just to make the point clear.
05:29Could you access physically your own heart?
05:34And let's say brain, because we were talking about heart all day.
05:38Could you physically access your brain?
05:40No, exactly.
05:46Could a cat access its heart, its whatever? No.
05:49That's exactly why we want to make things private from ourselves as well.
05:53When we're inside of the blueprint, right?
05:55When we're inside of the cat, then the cat needs to have access to the heart,
05:59and the brain needs to have access to it, and all these good stuff.
06:02When we're in the blueprint, that's what we are right now.
06:04We're in that blueprint. While we're in the blueprint, we have access to everything,
06:08no matter what, because we're inside of it.
06:10Now, when we say private, it's not private to the class.
06:13What is it private to?
06:14It's private to the instance of that class.
06:17So, if this was the cat class, and it had your private things,
06:21it would have access to everything inside it.
06:23But as soon as I create that cat, and we create that application,
06:26which we're going to do that in two weeks from now,
06:28after that application is created, anything that was private will be no access.
06:33No access to it, no one could enter it.
06:35Now, if we wanted something by default, everything is public.
06:38So, we don't even have to write public, but let's do that anyway.
06:41So, let's say we have a public variable, which we know what variables are.
06:45And let's say the public variable is sleeping,
06:50which is going to be a Boolean, and the value by default is going to be false.
06:53So, we have an is sleeping variable, which is false.
06:56Now, let's say we have another variable, which let's start it to be public as well in the beginning.
07:01And we're going to call it is eating, which is going to be a Boolean as well.
07:06And this one is going to actually be true.
07:08So, the cat is now, well, our main, very abstract, very not real in this scenario.
07:14So, our main is now eating, but it's not sleeping.
07:16Now, these are all operations that probably the outer world has a right to know about if the cat is sleeping, if it's eating, and so forth.
07:24But they're stuff that no one else should have access to.
07:27Let's say we have another variable, and let's think about saying that no one else should have access to besides the thing itself while it's doing it.
07:33Let's say is processing food.
07:38All right, so it's a Boolean.
07:42It says true because it's eating right now.
07:45So, it's true.
07:46It is processing food.
07:47Should I externally know that the cat is processing food?
07:50Absolutely not.
07:51So, that probably shouldn't be public, and then I'll make it private.
07:54Now, that's one layer.
07:56Now, what we talked about, and we said, first of all, before I even go into how do we differentiate between these two, because they look the exact same thing.
08:02When I'm working here, how do I know what's part of my API, what's not part of my API?
08:07And we said APIs are what Adam said, and I need to scroll it, because I never remember these acronyms.
08:13Or Danny said that, Application Programming Interface.
08:16So, it's called Application Programming.
08:18That's basically the things that we're allowed to interact with that specific instance object, whatever you want to call it for now.
08:28Why do we want to differentiate between them?
08:31One is because it makes it easier if we want to make sure that this is something that others have access to or not.
08:37Fair enough.
08:39Now, the way we would differentiate between them is very easy.
08:42What I like doing is adding, and this is standard, you see it quite often, is just adding in a lowercase, an underscore.
08:51This underscore basically makes it easy for me to say that this is a private variable.
08:56We don't tend to do that with functions.
08:59I don't know why specifically, but I've never really seen people put an underscore before their function, even if it's a private function.
09:05But it's a standard for variables, it makes sense.
09:08For functions, I think the main reason is because you usually have documentations.
09:14To be honest, I don't have a good reason for it.
09:16Well, I would think that if you really want to put also for private functions an underscore, there's nothing wrong with it.
09:22But usually it's not a standard. People usually don't do it.
09:26But that doesn't mean you shouldn't do it.
09:28If that makes it easier for you to recognize a public and private function, then by all means do it.
09:35To be honest, in moments of honesty, I don't 100% fully always support that.
09:43I don't always put that underscore before.
09:45I really do that only in very specific scenarios where we're going to be talking about in the future, which are called getters and setters.
