In the last two videos, we spoke about some basic topics around in classes that for the most part are
probably going to be at least a little bit familiar if you have covered 2015 before.
So now we're going to start to discuss some of the differences between 2015 classes and classes in TypeScript.
So the first thing we're going to cover is the existence of something called modifiers.
Modifiers are keywords that we can place on different methods and properties inside of a class, the
different options are public, private and protected.
The goal of these different modifiers is to restrict access to different functions or different variables
by default.
Every different method and property that we add to a class is always going to have the public, simply
the public modifier on it.
Now, the reason I say by default is that right now you'll notice we don't have anything that looks
like public, private or protected inside of our class.
So because we did not add anything or any of those modifiers inside of here, it is assumed that these
are all currently public.
So what does it mean to actually be public?
Well, it means that a given method or property, because we can add a modifier on a property as well,
but we'll discuss that in a little bit.
It means that method can be called anywhere, any time.
It's easiest to understand what public means if we then look first at private and protected, if a method
is marked as private, it means that that method can only be called by other methods in the exact same
class definition.
If a method is marked as protected, it means it can be called by other methods in that class and by
other methods in child classes as well.
By far, the easiest way to understand all this is to just write out code samples, so let's do that
right now.
So right now, back inside of my editor, I'm going to add on the public modifier to all three of these
different methods, you see, I'm going to say public, public.
By doing so, I have changed absolutely nothing inside of my code, again, by default, all these different
modifiers are considered to be public.
So in general, it's only when we start to add on different modifiers like protected or private, that
we're going to see any difference.
Let's try marking the drive method right here as private to get started.
So I'm going to add on the private modifier to that.
As soon as I do so, you're going to see an air appear on car drive, it says property drive is private
and only accessible within class car.
So that means that we can never call this method from outside of this class.
Only methods inside this class can actually call drive.
So let's say, for example, that we had a new method to this thing called start driving process.
And I'm going to have that with a return type, avoid this method right here, because it is inside
of a car, can safely call the drive method by referring to this drive like so.
So now down here on Khadar Drive, rather than calling car drive, we could instead call start driving
process.
We can safely call start driving process because it does not have the private modifier on it.
And start driving process can safely call, drive, even though drive is marked as private because is
an inside of the same class.
You'll notice we've got a little air right here if you hover over it.
We're seeing this air because we have added in a new property or we've modified a property in class
car that is also existing inside a vehicle as well.
But in vehicle, our definition up here drive is marked as public.
So the air here is that we've got a child class that has a different modifier than the parent class.
If we ever are overriding a method, as we are right here, we cannot change the modifier in the child
class.
So in this example, if we remove private life so that air will go away.
However, if vehicle did not have that method at all, we are no longer overriding it inside of car.
And so now we can successfully add on private like so.
OK, so, again, we're going to use private whenever we want to restrict access to a function.
Now, here's the really important thing, like why do we care about this?
Right.
That's the real important definition.
The first thing I want to tell you and make extremely clear is that we do not mark Method's as being
private over any type of security concern around like being hacked or something like that or having
some malicious user.
We need to find a method as being private, we are not adding in any layer of application security.
The only reason to mark something as private is to restrict the different methods that other developers
can call.
And the reason we want to do that is if we might have a method that very deeply manipulates a class
and maybe there's some, like, really complicated special way to use it.
And maybe you and I don't trust other developers to call that method safely.
If other developers call a given method, they might just inadvertently break something inside of application.
And so if that's the case, if we have some method that we just don't want other developers to call
because it's very likely they're going to break something, we will mark it as being private.
OK, now we've taken a look at private, let's also discuss what protected does, so protecting is just
like private.
The only difference is that with protected, we can access the given method in child classes as well.
So, for example, let's go back up to vehicle right here, rather than saying that Honk is going to
be public, let's make it as private really quickly.
And then let's say that when we start the driving process, we're not only going to do a drive, but
we'll also do a honk.
So when we try to call Huncke, our class is going to automatically try to call this method, but because
this thing is marked as private and is in a parent class, we are not allowed to do so.
So is only accessible within vehicle.
If we still actually really wanted to call honk, even though it's inside of a parent class, we could
mark it as protected.
So now that child class can access that method, but it cannot be accessed by anywhere outside of that
child class.
So, for example, if we now made an instance of vehicle right here, so consed vehicle.
His new vehicle.
We would not be allowed to call huncke.
If I try to do so well, very quickly, I see an error message once again.
OK, so that's the basics of modifiers, so we've now taken a look at basics of classes, methods,
a very tiny bit around inheritance and modifiers as well.
So quick pause right here.
We're going to start to cover fields in the next section.
So I'll see you in just a minute.