1
00:00:00,620 --> 00:00:05,360
In this video, we're going to continue discussing classes in typescript by taking a look at the inheritance

2
00:00:05,360 --> 00:00:05,890
system.

3
00:00:06,740 --> 00:00:11,090
So right underneath my class vehicle, I'm going to get started by creating a second class definition.

4
00:00:11,660 --> 00:00:13,580
I'm going to call this thing car like so.

5
00:00:14,440 --> 00:00:19,010
I want you to think about the relationship between a car and a vehicle in the real world.

6
00:00:19,360 --> 00:00:24,910
I think that we would say that a car is a type of vehicle and chances are that a whatever a car can

7
00:00:24,910 --> 00:00:27,010
do, a vehicle should be able to do as well.

8
00:00:27,430 --> 00:00:31,770
So, for example, a car should be able to drive and it should probably be able to honk as well.

9
00:00:32,650 --> 00:00:39,040
So we would probably want our class car to have essentially all the same methods or functions of class

10
00:00:39,040 --> 00:00:41,260
vehicle to make that happen.

11
00:00:41,290 --> 00:00:46,900
We could either copy paste all these methods down here like so, or alternatively, we could have our

12
00:00:46,900 --> 00:00:51,070
class car extend the vehicle class to do so.

13
00:00:51,080 --> 00:00:52,870
I'm going to find class car right here.

14
00:00:52,870 --> 00:00:59,710
And then right after I say extends vehicle like so I writing in this little additional syntax right

15
00:00:59,710 --> 00:01:04,300
here, we're essentially telling TypeScript that we wanted to take all the different methods inside

16
00:01:04,300 --> 00:01:08,300
of this class and essentially copy paste them over to class car.

17
00:01:09,100 --> 00:01:11,790
Now, there's not actually a real copy paste going on here.

18
00:01:11,950 --> 00:01:16,690
The actual system is a little bit more complicated and we're going to go deeper into that system over

19
00:01:16,690 --> 00:01:17,070
time.

20
00:01:17,440 --> 00:01:22,360
But right now, we can just imagine that by adding on extends vehicle, we're essentially taking all

21
00:01:22,360 --> 00:01:24,940
these methods and copy paste them down like so.

22
00:01:28,620 --> 00:01:33,420
Where we create a new vehicle down here, rather than creating a vehicle, we can delete all that and

23
00:01:33,420 --> 00:01:39,420
instead let's try creating an instance of a car so I can say consed car is new, car like so.

24
00:01:40,050 --> 00:01:44,820
And then on this instance of the class, we can now call all the different methods that we had associated

25
00:01:44,820 --> 00:01:50,100
with vehicle so we can say car drive and car don't honk like so.

26
00:01:51,250 --> 00:01:56,230
Now, if I say this file, flip back over to my terminal and run the file again, we're going to see

27
00:01:56,230 --> 00:01:57,700
the same output once again.

28
00:01:58,890 --> 00:02:04,170
When we have a class that extends another, we can optionally choose to override different methods.

29
00:02:04,680 --> 00:02:09,419
So, for example, let's say that we want our car class right here to have a different implementation

30
00:02:09,419 --> 00:02:11,220
of drive than the parent class.

31
00:02:12,200 --> 00:02:16,190
To do so, we can simply redefine drive inside of car.

32
00:02:17,540 --> 00:02:22,400
I'm going to again annotate this with the return type of void and maybe inside there for our car, rather

33
00:02:22,400 --> 00:02:24,110
than doing a console log of chug, chug, chug.

34
00:02:24,320 --> 00:02:28,670
I instead wanted to do a console log of, like, vroom like so.

35
00:02:30,160 --> 00:02:34,960
So if we now say this again and flip back over to our terminal, we can rerun the file once more and

36
00:02:34,960 --> 00:02:38,290
no longer are we going to do the Chugalug and we'll see Vroom instead.

37
00:02:39,130 --> 00:02:43,540
So we've successfully overridden a method on the child class.

38
00:02:43,960 --> 00:02:49,090
We refer to CAR as a child class because it is extending the vehicle.

39
00:02:49,630 --> 00:02:53,410
We would refer to a vehicle in this case as the superclass or the parent class.

40
00:02:55,190 --> 00:02:59,150
All right, now we've taken a look at basic inheritance, let's take another quick pause right here.

41
00:02:59,300 --> 00:03:02,120
We're going to address a couple more side topics around classes.

