1 00:00:00,710 --> 00:00:05,330 In this video we're going to continue discussing classes in typescript by taking a look at the inheritance 2 00:00:05,390 --> 00:00:06,800 system. 3 00:00:06,800 --> 00:00:11,670 So right underneath my class vehicle I'm going to get started by creating a second class definition. 4 00:00:11,780 --> 00:00:14,320 I'm going to call this thing car like so. 5 00:00:14,550 --> 00:00:19,460 I want you to think about the relationship between a car and a vehicle in the real world. 6 00:00:19,470 --> 00:00:24,900 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,900 --> 00:00:27,470 do a vehicle should be able to do as well. 8 00:00:27,480 --> 00:00:31,760 So for example a car should be able to drive and it should probably be able to honk as well. 9 00:00:32,700 --> 00:00:39,030 So we would probably want our class car to have essentially all the same methods or functions of class 10 00:00:39,030 --> 00:00:41,300 vehicle to make that happen. 11 00:00:41,310 --> 00:00:47,250 We could either copy paste all these methods down here like so or alternatively we could have our class 12 00:00:47,250 --> 00:00:51,080 car extend the vehicle class to do so. 13 00:00:51,120 --> 00:00:58,080 I'm going to find a class car right here and then right after I am say extends Vehicle like so by writing 14 00:00:58,080 --> 00:01:02,550 in this little additional syntax right here re essentially telling typescript that we wanted to take 15 00:01:02,640 --> 00:01:09,210 all the different methods inside of this class and essentially copy paste them over to class car. 16 00:01:09,210 --> 00:01:11,940 Now there's not actually a real copy paste going on here. 17 00:01:12,000 --> 00:01:16,680 The actual system is a little bit more complicated and we're going to go deeper into that system over 18 00:01:16,680 --> 00:01:17,310 time. 19 00:01:17,520 --> 00:01:22,530 But right now we can just imagine that by adding on extends Vehicle we're essentially taking all these 20 00:01:22,530 --> 00:01:26,840 methods and copy pasting them down like so. 21 00:01:26,880 --> 00:01:32,840 So now where we create a new vehicle down here rather than creating a vehicle we can delete all that 22 00:01:33,240 --> 00:01:40,400 and instead let's try creating an instance of a car so I can say const car is new car like so and then 23 00:01:40,460 --> 00:01:45,110 on this instance of the class we can now call all the different methods that we had associated with 24 00:01:45,140 --> 00:01:53,200 vehicle so we can say car drive and car Don't honk like so now if I say this file flipped backward my 25 00:01:53,200 --> 00:01:59,890 terminal and run the file again we're going to see the same output once again when we have a class that 26 00:01:59,890 --> 00:02:04,210 extends another we can optionally choose to override different methods. 27 00:02:04,750 --> 00:02:09,520 So for example let's say that we want our car class right here to have a different implementation of 28 00:02:09,520 --> 00:02:17,480 drive than the parent class to do so we can simply redefine drive inside of car. 29 00:02:17,660 --> 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 30 00:02:22,400 --> 00:02:30,150 than doing a console log of CHUG CHUG A I instead wanted to do a console log of like vroom like so so 31 00:02:30,330 --> 00:02:35,460 if we now say this again and flip back over to our terminal we can rerun the file once more and no longer 32 00:02:35,490 --> 00:02:36,810 are we going to do that chug chug. 33 00:02:36,840 --> 00:02:39,030 And we'll see vroom instead. 34 00:02:39,160 --> 00:02:47,160 So we've successfully overridden a method on the child class we refer to talk car as a child class because 35 00:02:47,160 --> 00:02:53,430 it is extending vehicle we would refer to vehicle in this case as the superclass or the parent class 36 00:02:55,300 --> 00:02:55,570 all right. 37 00:02:55,570 --> 00:02:57,360 Now we've taken a look at basic inheritance. 38 00:02:57,380 --> 00:02:59,260 Let's take another quick pause right here. 39 00:02:59,350 --> 00:03:02,140 We're going to address a couple more side topics around classes.