1
00:00:01,650 --> 00:00:02,880
Hey what's up everyone.

2
00:00:02,880 --> 00:00:04,910
Mark Price you're a dove slopes dot com.

3
00:00:04,920 --> 00:00:11,950
And today we're going to talk about inheritance not like the inheritance you got from your grandfather.

4
00:00:12,060 --> 00:00:16,530
You know when all your peers were out there working their butts off you know failing and failing and

5
00:00:16,530 --> 00:00:20,730
failing again and their Swetnam bleeding and you know you're out there and you get an inheritance and

6
00:00:20,730 --> 00:00:22,860
you're just like here's my Bugatti.

7
00:00:22,860 --> 00:00:23,710
You guys suck.

8
00:00:23,970 --> 00:00:25,570
Yes not one of those kind of things.

9
00:00:25,740 --> 00:00:26,450
It's a little bit different.

10
00:00:26,450 --> 00:00:34,230
It's more so like an inheritance of features more like I have red hair because my grandfather has red

11
00:00:34,230 --> 00:00:34,510
hair.

12
00:00:34,530 --> 00:00:38,360
You've inherited some type of features from some type of relative.

13
00:00:38,430 --> 00:00:47,280
So I'm going to get started with the playground and we're going to call this in heritance inheritance

14
00:00:47,290 --> 00:00:49,750
it's one of those words that's easy to misspell.

15
00:00:50,040 --> 00:00:53,330
Inheritance is a principle of object oriented programming.

16
00:00:53,520 --> 00:00:54,000
OK.

17
00:00:54,000 --> 00:00:55,250
Object oriented programming.

18
00:00:55,250 --> 00:01:01,290
So a perfect example is continuing on with a car or a vehicle.

19
00:01:01,350 --> 00:01:06,670
We've got to view class and a vehicle class has some basic features list.

20
00:01:06,700 --> 00:01:10,240
We'll go ahead and say like var tires equals four.

21
00:01:10,290 --> 00:01:16,480
You've seen something like this before and then bar you know make of type string.

22
00:01:16,500 --> 00:01:20,350
But right now there's nothing in a bar model of type string.

23
00:01:20,580 --> 00:01:21,170
OK.

24
00:01:21,390 --> 00:01:33,810
And we said phunk drive and then we can say phunk for aik OK and save our current speed of type let's

25
00:01:33,810 --> 00:01:34,870
say double.

26
00:01:35,600 --> 00:01:36,890
And we started off at zero.

27
00:01:36,900 --> 00:01:37,980
The car's not moving.

28
00:01:38,250 --> 00:01:41,330
And so may be OK.

29
00:01:42,120 --> 00:01:46,350
Let's say let's say you are creating can complex drive algorithm where it's going to move accelerate

30
00:01:46,350 --> 00:01:47,280
the car forward.

31
00:01:47,460 --> 00:01:56,210
So we pass in the speed increase of type double and then maybe the base.

32
00:01:56,360 --> 00:02:03,690
So for a basic car the basic algorithm is this it is this current speed OK.

33
00:02:04,130 --> 00:02:10,330
Plus equals speed increase times.

34
00:02:10,490 --> 00:02:16,850
Let's say two let's say that was the algorithm some random arbitrary algorithm.

35
00:02:17,000 --> 00:02:22,930
The current speed of the vehicle equals the current speed plus speed increase times two.

36
00:02:23,070 --> 00:02:23,930
OK.

37
00:02:24,350 --> 00:02:27,680
Now this going and create what's called a subclass.

38
00:02:27,680 --> 00:02:30,610
OK so this is a class let's create a subclass.

39
00:02:30,770 --> 00:02:32,390
We're going to show inheritance here.

40
00:02:32,570 --> 00:02:33,740
So a class

41
00:02:37,250 --> 00:02:40,020
what do I want to say class BMW.

42
00:02:41,860 --> 00:02:46,140
OK class BMW or even better.

43
00:02:46,140 --> 00:02:47,760
How about a sports car.

44
00:02:47,780 --> 00:02:55,100
Just for fun sports car inherits from vehicle so you start inheritance by putting a colon here and then

45
00:02:55,100 --> 00:02:58,600
specifying the type K which is this.

46
00:02:58,600 --> 00:03:02,930
This is the type the type or class that you want to inherit from.

47
00:03:02,930 --> 00:03:05,520
Now this is really cool there's nothing in here right nothing at all.

48
00:03:05,720 --> 00:03:11,570
But you can still access the properties of the parent class so on this sports car you know tires are

49
00:03:11,570 --> 00:03:12,340
going to stay the same.

50
00:03:12,350 --> 00:03:14,680
But you know maybe we can set the make and model.

51
00:03:14,780 --> 00:03:21,740
So I don't have to recreate those which is really cool I can say make equals and let's say BMW and model

52
00:03:21,830 --> 00:03:25,440
equals the say three series.

53
00:03:25,460 --> 00:03:25,970
OK.

54
00:03:26,330 --> 00:03:31,040
So we've got to make an a model.

55
00:03:31,440 --> 00:03:39,490
What we need to do here actually is create an initialiser first like so.

56
00:03:40,310 --> 00:03:45,770
All right and then and here we can set our make and our model like so is yelling at us because it wants

57
00:03:45,770 --> 00:03:52,940
us to override it.

58
00:03:53,230 --> 00:04:01,700
So we need to call super in it super It just some boilerplate stuff you've got to do in every class.

59
00:04:01,870 --> 00:04:02,270
OK.

60
00:04:02,440 --> 00:04:07,120
So every single class has by default an initialization function but you don't see it right here because

61
00:04:07,120 --> 00:04:08,520
we did not implement it.

62
00:04:08,770 --> 00:04:10,170
So let's implement it.

63
00:04:10,170 --> 00:04:14,540
Now on the parent class OK.

64
00:04:14,910 --> 00:04:17,920
So we've got to initialize initialiser on the parent class.

65
00:04:17,940 --> 00:04:20,610
And initialiser here in the subclass.

66
00:04:20,610 --> 00:04:22,410
And this is interesting.

67
00:04:22,500 --> 00:04:24,960
We're calling super init on the parent class.

68
00:04:24,960 --> 00:04:25,700
What does that even mean.

69
00:04:25,710 --> 00:04:27,800
Well let's print this out.

70
00:04:27,810 --> 00:04:30,260
I am the parent.

71
00:04:30,700 --> 00:04:31,040
OK.

72
00:04:31,040 --> 00:04:33,220
And then just print this down here.

73
00:04:34,200 --> 00:04:42,180
I am the child and what I want to do is just create an instance of this class penning the X code doesn't

74
00:04:42,180 --> 00:04:44,290
crash on us.

75
00:04:44,700 --> 00:04:45,930
It crashed.

76
00:04:46,200 --> 00:04:48,350
So OK.

77
00:04:48,510 --> 00:04:54,110
But I want to do is create an instance of the sports car so let car equals a new sports car.

78
00:04:54,990 --> 00:04:56,580
And what's interesting.

79
00:04:58,220 --> 00:05:02,800
Fit aside soloed I am the parent Prince and I am the child of what's happening here.

80
00:05:02,840 --> 00:05:08,060
So we create the sports car and then this initialiser is called and the override function says I want

81
00:05:08,060 --> 00:05:10,190
to override the parents initialiser function.

82
00:05:10,250 --> 00:05:15,120
So it won't be called at all but then we actually call here explicitly.

83
00:05:15,180 --> 00:05:19,200
So then that's going to go back up to the parent class and call was whatever in it.

84
00:05:19,220 --> 00:05:23,020
So sometimes parent classes have set up or things like that and introduce We want to call it.

85
00:05:23,060 --> 00:05:28,560
It's typical to call the parent class and then to create your own functionality which is really cool.

86
00:05:28,790 --> 00:05:31,290
OK so here's our sports car.

87
00:05:31,340 --> 00:05:31,700
Awesome.

88
00:05:31,700 --> 00:05:35,990
Now let's our sports car needs to drive faster than the standard algorithm.

89
00:05:35,990 --> 00:05:41,000
So what we can do is we can even override the functions so I can type and drive.

90
00:05:41,130 --> 00:05:42,590
That's going to override it.

91
00:05:42,650 --> 00:05:48,500
And now what I could do instead of current speed being equal to this I can say current speed plus equals

92
00:05:48,590 --> 00:05:52,420
speed increase times three.

93
00:05:52,430 --> 00:05:55,330
So this one is times too but this was times three.

94
00:05:55,400 --> 00:06:00,690
And now the child's class we know that every car needs to have a drive function.

95
00:06:00,690 --> 00:06:01,570
And so that's great.

96
00:06:01,580 --> 00:06:05,300
Like why would we we wouldn't want to create a bunch of different classes and re-implement this function

97
00:06:05,330 --> 00:06:06,410
every single time.

98
00:06:06,620 --> 00:06:10,520
So it's already pre-built and we just wrote it once and we overwrote it here which is great.

99
00:06:10,550 --> 00:06:16,550
Every vehicle needs to be able to drive but its implementation could be different purview.

100
00:06:16,580 --> 00:06:21,570
Is a Ford F-150 going to accelerate differently than a Ford Mustang.

101
00:06:21,650 --> 00:06:24,160
Yes every car is going to have its acceleration.

102
00:06:24,320 --> 00:06:28,560
Therefore it would have its own code here in the functions that it's overriding.

103
00:06:28,580 --> 00:06:31,470
And of course you can override the properties as well too.

104
00:06:31,790 --> 00:06:36,420
So the whole point of this lesson we could go way down this rabbit hole where we're not going to.

105
00:06:36,560 --> 00:06:42,500
The whole point of this lesson to know is that you can create a parent class and children classes can

106
00:06:42,500 --> 00:06:45,590
inherit from that parent class and you can do more than one.

107
00:06:45,590 --> 00:06:50,900
So if we bought a sports car we could say class truck inherits from view acle we could have done the

108
00:06:50,900 --> 00:06:52,330
exact same thing there.

109
00:06:52,520 --> 00:06:58,210
And then you know initialiser or let's let's just copy what we got here.

110
00:06:58,260 --> 00:07:01,120
Let's make it real easy so it doesn't yell at us.

111
00:07:01,220 --> 00:07:01,730
OK.

112
00:07:01,970 --> 00:07:07,880
And you know we can get rid of these here OK and then a trunk a truck could have its own drive function

113
00:07:08,270 --> 00:07:18,270
that you know speed increase you know plus equals or no excuse me current speed plus equal speed increase.

114
00:07:18,290 --> 00:07:20,890
You know without multiplying it by no multiplier.

115
00:07:21,170 --> 00:07:25,820
And so anyway now we've got two different classes here that both inherit from Buca they both have a

116
00:07:25,820 --> 00:07:31,760
drive function but their implementation is different and why this is important is because it helps you

117
00:07:32,630 --> 00:07:36,800
create objects that can adapt to different situations.

118
00:07:36,800 --> 00:07:43,640
For instance you know on Instagram you may have a parent class that has you know a specific base filter

119
00:07:43,640 --> 00:07:47,570
but then you may have different filters like Valencia and other things that have their own algorithms

120
00:07:47,990 --> 00:07:49,240
that they need to implement.

121
00:07:49,370 --> 00:07:54,230
So rather than putting it all one giant class file it could easily have created a parent class which

122
00:07:54,230 --> 00:07:58,190
creates a deeper filter and then they can override it and add some other functionality to it for those

123
00:07:58,190 --> 00:07:59,690
specific filters.

124
00:07:59,810 --> 00:08:04,850
Again all kinds of things you can do with classes objects and inheritance but that's all you need to

125
00:08:04,850 --> 00:08:09,590
know now you're going to learn about it more as you use it in Aiwass development so.

126
00:08:09,980 --> 00:08:12,250
Mark Price your DENSELOW dot com.

127
00:08:12,290 --> 00:08:14,660
Movin on and.
