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