Hello, in this video, I am going to cover the difference between public, private and protected. So this is to do with scope. And as you can see, everything is public so far, this was so easy to access before I explained what those key words meant, instead of the BMW object. I'm going to create a class our car we don't really need BMW, the BMW one for you know this case, we just need to, but the principle is the same. And I'm going to create a car I'll be doing all over OBJ equals new car. And now we're going to do is let's call this function move forward.
So move forward Running advocacy runs the move afford function. So, the difference in public protected and private is public can be used within this class in our life D variables, it can be used in any of the children classes, and it can also be used outside of the class like this. If I change this to quote Now, watch what happens. Well, yeah Naira is given us an error, because the same core to protected method. So this means this function can only be used within the function. So within within another function, I mean within the class within another function of this class, because it's protected.
It can also be used directly by its child class if he had one. So in the open door method, I can call move, board like so and If I was to comment this out, and now put call OBJ. Open door there we go decided to put, you know, dollar this to actually get its own functionality I forgot about that. As you can see, I can utilize it now even in the child class. And finally, the keyword private for that, again era, because private, wherever it's a variable, or wherever it's a function can only be used within the class that it is declared. So now that that is private, and I've tried to call it in the child class, it doesn't work because it's saying private can only be accessed with it.
So, recap public can be accessed within the classes declared in a child class and outside like so, as the same variable and a functions a protected, you know variable or function can be accessed within the class base declared, and any direct children classes. And then finally, private like dysfunction is made now, can only be used with in this particular class. So, you know, let's say, within the print all info, I could do something like you know, for example, so it will move it forward, if I use the print or inverse is one sort of way of getting around it. So, yeah, that is a difference between public private and protected. It's essentially to do with scope similar to when we were looking at variable, so scope, but to do with classes and you can do it on methods as well. So it's really important you always really want to lock down the scope as tight as possible.
So only, you know, make the scope as high or open as you really really need to just to prevent any, you know, accidental changes and you know, problems from occurring. That's really it for this video. If you have any questions, feel free to pop me a message. And as usual, I look forward to seeing you in the next tutorial.