1
00:00:00,810 --> 00:00:05,130
Now that we've got a better idea of how fields work inside of a class, let's discuss how they work

2
00:00:05,130 --> 00:00:09,180
when we are dealing with inheritance as we are with our class car down here.

3
00:00:10,270 --> 00:00:13,870
I'm going to uncommon everything in the bottom half this file right here, and as soon as I do, you'll

4
00:00:13,870 --> 00:00:17,050
notice we get an air around the new car call right here.

5
00:00:17,620 --> 00:00:18,490
So here's the issue.

6
00:00:18,970 --> 00:00:21,460
Our class car extends vehicle.

7
00:00:21,970 --> 00:00:27,970
Any time we create an instance of something that is a vehicle, we have to provide it a color or a string

8
00:00:27,970 --> 00:00:29,180
to use as the color.

9
00:00:30,010 --> 00:00:34,840
So the reason we're seeing this air right here is that we have to pass in a string that will serve as

10
00:00:34,840 --> 00:00:35,320
the color.

11
00:00:36,160 --> 00:00:39,460
So we could fix up this air by putting a string in like red like so.

12
00:00:39,490 --> 00:00:41,500
And as soon as we do that, the air goes away.

13
00:00:42,500 --> 00:00:48,360
I want to show you something interesting right now, our class card does not have a constructor function.

14
00:00:48,930 --> 00:00:53,880
That means that whenever we try to create an instance of car, typescript is going to automatically

15
00:00:53,880 --> 00:00:57,330
call the constructor in the parent function right here.

16
00:00:58,080 --> 00:01:03,270
So even though you don't see any reference to it, whenever we create this car right here, the constructor

17
00:01:03,270 --> 00:01:05,519
in the parent is being called for us automatically.

18
00:01:05,519 --> 00:01:07,380
And that's why we have to pass in that string.

19
00:01:08,230 --> 00:01:13,300
Things are going to be just a little bit different if we define the constructor inside of class car

20
00:01:13,330 --> 00:01:13,890
as well.

21
00:01:14,830 --> 00:01:19,870
So let's try that right now, I'm going to define the constructor function inside of car, and I'm going

22
00:01:19,870 --> 00:01:25,150
to say that this thing is going to have its own property like, let's say, public wheels.

23
00:01:25,150 --> 00:01:27,970
And this will be maybe like the number of wheels that a car has.

24
00:01:28,270 --> 00:01:30,760
So I'm going to annotate with the type of number like so.

25
00:01:32,250 --> 00:01:36,840
Now, as soon as I define this constructor, you'll notice Nasti Air inside of here, and if I hover

26
00:01:36,840 --> 00:01:40,290
over it, it says that the derived class must contain a super cool.

27
00:01:41,190 --> 00:01:46,590
The word super right here is a reference to these super class or the parent class of car, which is

28
00:01:46,590 --> 00:01:47,040
vehicle.

29
00:01:47,980 --> 00:01:52,330
So whenever we called a constructor of a child class, we are required to call the constructor method

30
00:01:52,450 --> 00:01:57,730
of the parent as well to do so, all we have to do is call super like so.

31
00:01:58,810 --> 00:02:04,690
As soon as we do so, we're still going to see an air here, so super right here is a reference to the

32
00:02:04,690 --> 00:02:06,590
constructor method in the parent.

33
00:02:06,970 --> 00:02:12,760
And once again, the constructor and the parent wants us to pass in a string so we can make this go

34
00:02:12,760 --> 00:02:15,670
away once again by putting in some hard coded string like so.

35
00:02:16,240 --> 00:02:17,740
But chances are we don't want to do that.

36
00:02:17,770 --> 00:02:23,650
Chances are we want to have the color still come in as an argument to our car when we create it.

37
00:02:24,460 --> 00:02:27,430
So maybe we should take in a second argument to the constructor function.

38
00:02:28,860 --> 00:02:32,280
That will call color, and it's supposed to be a string like so.

39
00:02:33,060 --> 00:02:38,310
Notice how I did not put the public key word on here I didn't put public on because we do not want to

40
00:02:38,400 --> 00:02:41,330
reassign or create a new field in car of color.

41
00:02:42,180 --> 00:02:44,520
The field color belongs to vehicle.

42
00:02:45,130 --> 00:02:47,910
So that's why I did not put on the modifier of public.

43
00:02:49,130 --> 00:02:53,960
So now when we call the parent class constructor through these supercool right here, we're not going

44
00:02:53,960 --> 00:02:55,120
to pass into hardcoded red.

45
00:02:55,340 --> 00:02:57,110
Instead, we can pass in the color.

46
00:02:58,370 --> 00:03:02,090
If we now go back down to the bottom of the file once more, you'll see we've got an area down here

47
00:03:02,090 --> 00:03:02,480
again.

48
00:03:03,230 --> 00:03:08,660
So now we are saying that our constructor for car has to be given a number of wheels as the first argument

49
00:03:08,750 --> 00:03:13,880
and a second argument of a string so we can fix up this error by putting in four wheels as the first

50
00:03:13,880 --> 00:03:14,310
argument.

51
00:03:14,620 --> 00:03:18,770
Now, the second argument is going to be a string, which is going to pass on through the constructor

52
00:03:18,770 --> 00:03:21,410
right here into the parent constructor of vehicle.

53
00:03:22,220 --> 00:03:23,120
And that's pretty much it.

54
00:03:24,140 --> 00:03:26,600
Now, I know we're going through this stuff really quickly.

55
00:03:26,810 --> 00:03:31,160
The reason we're going through it so quickly is that basically all the projects we're going to work

56
00:03:31,160 --> 00:03:37,230
on in this course are 100 percent focused on using classes to represent logic inside of application.

57
00:03:37,880 --> 00:03:41,660
So really, I'm just showing you all this class stuff right now to get you used to some of the very

58
00:03:41,660 --> 00:03:42,830
basic syntax.

59
00:03:43,100 --> 00:03:47,660
And we're going to really repeat all these different rules that we're going over in great detail throughout

60
00:03:47,660 --> 00:03:49,250
all these applications we go through.

61
00:03:49,850 --> 00:03:52,490
So hopefully you're just absorbing a little bit of the syntax here.

62
00:03:52,700 --> 00:03:54,710
That's really the only expectation I have.

63
00:03:55,680 --> 00:03:59,940
Now, having said all that, this is really all we wanted to learn about classes, so we've taken a

64
00:03:59,940 --> 00:04:05,790
look at Method's constructor's basic inheritance, super calls modifiers.

65
00:04:05,790 --> 00:04:07,650
That's pretty much it, the whole ball game.

66
00:04:08,200 --> 00:04:09,720
So let's have a quick pause right here.

67
00:04:09,750 --> 00:04:14,310
We're going to do a wrap up on classes in the next video, and we'll start working on some really interesting

68
00:04:14,310 --> 00:04:15,120
applications.

69
00:04:15,120 --> 00:04:16,790
So I'll see you in just a minute.

