In this video we're going to continue discussing classes in typescript by taking a look at the inheritance
system.
So right underneath my class vehicle I'm going to get started by creating a second class definition.
I'm going to call this thing car like so.
I want you to think about the relationship between a car and a vehicle in the real world.
I think that we would say that a car is a type of vehicle and chances are that a whatever a car can
do a vehicle should be able to do as well.
So for example a car should be able to drive and it should probably be able to honk as well.
So we would probably want our class car to have essentially all the same methods or functions of class
vehicle to make that happen.
We could either copy paste all these methods down here like so or alternatively we could have our class
car extend the vehicle class to do so.
I'm going to find a class car right here and then right after I am say extends Vehicle like so by writing
in this little additional syntax right here re essentially telling typescript that we wanted to take
all the different methods inside of this class and essentially copy paste them over to class car.
Now there's not actually a real copy paste going on here.
The actual system is a little bit more complicated and we're going to go deeper into that system over
time.
But right now we can just imagine that by adding on extends Vehicle we're essentially taking all these
methods and copy pasting them down like so.
So now where we create a new vehicle down here rather than creating a vehicle we can delete all that
and instead let's try creating an instance of a car so I can say const car is new car like so and then
on this instance of the class we can now call all the different methods that we had associated with
vehicle so we can say car drive and car Don't honk like so now if I say this file flipped backward my
terminal and run the file again we're going to see the same output once again when we have a class that
extends another we can optionally choose to override different methods.
So for example let's say that we want our car class right here to have a different implementation of
drive than the parent class to do so we can simply redefine drive inside of car.
I'm going to again annotate this with the return type of void and maybe inside there for our car rather
than doing a console log of CHUG CHUG A I instead wanted to do a console log of like vroom like so so
if we now say this again and flip back over to our terminal we can rerun the file once more and no longer
are we going to do that chug chug.
And we'll see vroom instead.
So we've successfully overridden a method on the child class we refer to talk car as a child class because
it is extending vehicle we would refer to vehicle in this case as the superclass or the parent class
all right.
Now we've taken a look at basic inheritance.
Let's take another quick pause right here.
We're going to address a couple more side topics around classes.