In the last two videos we spoke about some basic topics are in classes that for the most part are probably
going to be at least a little bit familiar if you have covered.
Yes 2015 before.
So now we're gonna start to discuss some of the differences between yes 2015 classes and classes in
typescript.
The first thing we're going to cover is the existence of something called modifiers modifiers are keywords
so 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 want to add on the public modifier to all three of these different
methods you see I'm going to say public 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 gonna add on the private
modifier to that as soon as I do so you're going to see an error appear on cart 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
gonna have that with the return type of void.
This method right here because it is inside of class car can safely call the drive method by referring
to this dot drive like so.
So now down here on car dot drive rather than calling card out drive we can 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 a drive is marked as private because is inset 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 are ever our overriding a method as we are right here we can not change the modifier in the child
class.
So in this example if we remove private like so that air will go away however if vehicle did not have
that method at all we are no longer overwriting it inside of car.
And so now we can successfully add on private like so okay.
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 they want to tell you and make extremely clear is that we do not mark methods as being private
over any type of security concern around like being hacked or something like that.
We're having some malicious user when you define a method as being private.
We are not adding in any layer of application security.
The only reason a marked 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 market as being private OK.
Now we've taken a look at private let's also discuss what protected does so protect it 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 hunk is going to be public.
Let's market it 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 hunk like so so when we try to call hunk.
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 Hank is only accessible within vehicle if we
still actually really wanted to call Hank.
Even though it's inside of a parent class we could market as protected.
So now that child class can access that method but it can not be accessed by anywhere outside of that
child class.
So for example if we now made an instance of vehicle right here.
So concept vehicle is new vehicle we would not be allowed to call hunk if I try to do so well very quickly
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 gonna start to cover fields in the next section so I'll see you in just a minute.