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 class car right here.
And then right after I say extends vehicle like so I writing in this little additional syntax right
here, we'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 paste them down like so.
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 consed 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, flip back over to 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, chug.
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 the Chugalug and we'll see Vroom instead.
So we've successfully overridden a method on the child class.
We refer to CAR as a child class because it is extending the vehicle.
We would refer to a 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.