1
00:00:01,500 --> 00:00:07,410
Welcome back again we're going to conclude our brief introduction into object oriented programming by

2
00:00:07,410 --> 00:00:10,180
topic but a blurb.

3
00:00:10,950 --> 00:00:12,980
By talking about polymorphism.

4
00:00:13,190 --> 00:00:14,050
OK.

5
00:00:14,500 --> 00:00:20,960
And man sometimes just the words I want to come out and they don't come out.

6
00:00:21,400 --> 00:00:25,740
But they came out supinely morphism that's a hard word and that's hard enough for it to say as it is

7
00:00:25,770 --> 00:00:31,470
and it's a question that's often asked in interviews for jobs which is funny.

8
00:00:31,590 --> 00:00:36,660
There was like can you please define polymorphism and you're like oh what do I say.

9
00:00:36,660 --> 00:00:39,590
Because the definition so hard the implementation of it is so easy.

10
00:00:39,600 --> 00:00:41,330
But the definition is pretty hard.

11
00:00:41,580 --> 00:00:47,380
So we're going to say define polymorphism define Pawley morphism

12
00:00:50,370 --> 00:00:53,710
the condition of occurring in several forms.

13
00:00:54,060 --> 00:00:58,620
The occurrence of different forms among the members of a population or colony or in the lifecycle of

14
00:00:58,620 --> 00:01:00,330
an individual organism.

15
00:01:00,330 --> 00:01:03,140
So it Socan this is just generic definitions right.

16
00:01:03,140 --> 00:01:07,980
Talking about having different forms occurring in several several different forms.

17
00:01:07,980 --> 00:01:11,810
Interesting right occurring in several different forms.

18
00:01:12,240 --> 00:01:13,240
Here's another one.

19
00:01:13,590 --> 00:01:15,800
Some Please find it in two sentences.

20
00:01:15,840 --> 00:01:20,690
Polymorphism allows the expression of some sort of contract with potentially many types.

21
00:01:20,700 --> 00:01:26,070
Implementing that contract whether through class inheritance or not in different ways each according

22
00:01:26,070 --> 00:01:31,560
to their own purpose coding code using that contract should not have to care about which implementation

23
00:01:31,620 --> 00:01:35,250
is involved only that the contract that the contract will be obeyed.

24
00:01:35,250 --> 00:01:38,980
I like that one a lot and this is a programming definition.

25
00:01:39,150 --> 00:01:45,530
So you're going to create a contract and then different things can implement that contract.

26
00:01:45,540 --> 00:01:48,860
But they do it in their own way and you're probably super confused right now.

27
00:01:48,870 --> 00:01:52,060
And you should be because that's the textbook definition that's compute.

28
00:01:52,050 --> 00:01:54,190
That's what they teach you in college.

29
00:01:54,200 --> 00:01:58,980
All right let's see if we can confuse our students and not teach them anything practical but teach them

30
00:01:58,980 --> 00:02:01,760
things that don't make any sense.

31
00:02:02,160 --> 00:02:07,800
So let's teach you whether or what they're all about get started with the playground here and we're

32
00:02:07,800 --> 00:02:09,380
going to call this one.

33
00:02:09,380 --> 00:02:12,290
Paul Lee more fearsome.

34
00:02:13,100 --> 00:02:14,700
OK put it here in swithe.

35
00:02:14,700 --> 00:02:16,740
That's great.

36
00:02:16,740 --> 00:02:20,360
The principle is so simple once you watch me it is going to be like that.

37
00:02:20,760 --> 00:02:25,200
And that's how you should lead in your interview questions by talking about.

38
00:02:25,230 --> 00:02:26,870
Well let me just give you an example instead.

39
00:02:26,940 --> 00:02:32,540
So what we're going to do here is we're going to create a class called Shape.

40
00:02:32,960 --> 00:02:33,690
OK.

41
00:02:33,780 --> 00:02:39,810
And we're going to a property in it called Area OK bar area of type double.

42
00:02:39,930 --> 00:02:44,850
It may or may not have a value and this will put an optional at the end of it and create a function

43
00:02:44,850 --> 00:02:50,310
called calque relate area to.

44
00:02:50,480 --> 00:02:51,350
OK.

45
00:02:51,810 --> 00:02:57,450
Now here's our base class and every shape is going to have a calculate area function.

46
00:02:57,570 --> 00:03:02,440
Ok so here's the advantage of working with a base class.

47
00:03:02,520 --> 00:03:09,390
For instance let's say we didn't do this and what I wanted to get rectangles area so I would say class

48
00:03:10,260 --> 00:03:14,250
and then I would have to go and see a rectangle.

49
00:03:14,730 --> 00:03:15,390
OK.

50
00:03:15,440 --> 00:03:18,860
And then I'd say our area of type double.

51
00:03:19,050 --> 00:03:22,190
And then I would say phunk calculate area.

52
00:03:22,740 --> 00:03:23,600
OK.

53
00:03:24,030 --> 00:03:25,810
And then area would be.

54
00:03:26,000 --> 00:03:28,140
And then we need to pass and to lengthen it with here right.

55
00:03:28,140 --> 00:03:34,270
So laying type double and width of type double.

56
00:03:34,620 --> 00:03:39,160
And then we would say area equals length times with.

57
00:03:39,560 --> 00:03:40,370
OK.

58
00:03:40,920 --> 00:03:46,460
And then all of a sudden the boss was like oh no we need to also create a triangle so we're like OK.

59
00:03:46,590 --> 00:03:56,590
Like triangle or like var area double and then we're like phunk calculate area.

60
00:03:57,180 --> 00:03:59,540
And then we get a base of type of all.

61
00:03:59,780 --> 00:04:03,560
And what is a base and height divided by two.

62
00:04:03,600 --> 00:04:04,280
I think so.

63
00:04:04,440 --> 00:04:12,240
So we get the height which is a double and then so we say you know area equals based times height divided

64
00:04:12,240 --> 00:04:16,920
by two and one just puts in print this is peroration.

65
00:04:17,380 --> 00:04:21,580
OK so we've got two classes both doing similar things.

66
00:04:21,590 --> 00:04:22,440
But look at this.

67
00:04:22,550 --> 00:04:26,000
We had to recreate the area property we had to recreate this property here.

68
00:04:26,120 --> 00:04:31,250
And you know what happens if in this case one programmer gets lazy and they're like calque area and

69
00:04:31,250 --> 00:04:35,590
this one is just like you know let's get area.

70
00:04:35,600 --> 00:04:38,800
So now things are becoming inconsistent and the parameters start changing.

71
00:04:38,800 --> 00:04:40,490
You know base value.

72
00:04:40,490 --> 00:04:42,920
And this is not really good.

73
00:04:42,950 --> 00:04:44,020
We're rewriting code.

74
00:04:44,030 --> 00:04:45,470
We can break our logic.

75
00:04:45,470 --> 00:04:48,030
This is all trash trash trust trash.

76
00:04:48,110 --> 00:04:49,160
Get rid of it you're fired.

77
00:04:49,160 --> 00:04:50,540
Can't work here anymore.

78
00:04:51,050 --> 00:04:51,580
I'm just kidding.

79
00:04:51,590 --> 00:04:52,790
I'd still hire you.

80
00:04:53,240 --> 00:04:53,540
OK.

81
00:04:53,540 --> 00:04:55,490
So how do we fix this problem.

82
00:04:55,490 --> 00:05:00,620
How to make things more consistent where we can use polymorphism and what we're going to do now instead

83
00:05:00,710 --> 00:05:04,500
is we're going to create a new class called triangle of course.

84
00:05:04,610 --> 00:05:11,840
But this time we're going to inherit from Shape OK inherit from shape and all we have to do is obey

85
00:05:11,840 --> 00:05:13,940
the contract which is calculate area.

86
00:05:13,940 --> 00:05:19,280
So what we can do is we can say I want to implement calculate area function and we override it completely

87
00:05:19,910 --> 00:05:21,800
and then all we have to do.

88
00:05:22,070 --> 00:05:23,220
Actually this is wrong.

89
00:05:23,300 --> 00:05:31,250
This needs to have the values in it so let's put a value of type double and value be of type double.

90
00:05:31,820 --> 00:05:32,520
OK.

91
00:05:32,660 --> 00:05:42,660
Now we can calculate the area like so and then all we need to do is say area equals valet or times Balbi

92
00:05:43,290 --> 00:05:49,320
like so divided by two and this is this and this is this and this is a try.

93
00:05:51,690 --> 00:05:55,460
And in this case over here we want a rectangle class Rectangle.

94
00:05:56,160 --> 00:06:00,340
And that's going to inherit from shape and we do the same thing here calculate area.

95
00:06:00,450 --> 00:06:06,560
But in this case area equals value a times vel B.

96
00:06:07,080 --> 00:06:08,430
So that's interesting.

97
00:06:08,570 --> 00:06:10,800
We've got two different shapes here.

98
00:06:11,180 --> 00:06:15,960
Implementing the exact same function but the logic inside is different and that is polymorphism.

99
00:06:15,960 --> 00:06:16,930
OK.

100
00:06:17,160 --> 00:06:25,710
This is one form ok with or this is one object or form of an object but that is taking different forms

101
00:06:26,490 --> 00:06:27,410
like so.

102
00:06:27,720 --> 00:06:33,840
OK so we were obeying the contract like we saw in the really great stack overflow definition our obeying

103
00:06:33,840 --> 00:06:37,260
the contract and both these cases but they're both doing something different.

104
00:06:37,500 --> 00:06:39,280
And we're still using the same parent class.

105
00:06:39,300 --> 00:06:41,850
And that's what polymorphism and you use it all the time.

106
00:06:41,850 --> 00:06:47,430
So next when you go in your job interview if you can't read the definition you're like well let me just

107
00:06:47,430 --> 00:06:48,390
give you an example.

108
00:06:48,450 --> 00:06:51,540
So when he says give me the definition of polymorphous and said Let me give you an example.

109
00:06:51,540 --> 00:07:00,030
Imagine that you have a shape class and you have to calculate the area of shape but you don't know which

110
00:07:00,030 --> 00:07:02,390
shape is going to be passed in at runtime.

111
00:07:02,580 --> 00:07:11,310
So food critic calculate area function and then we also create two more classes like a triangle or rectangle

112
00:07:11,730 --> 00:07:12,600
that inherit from shape.

113
00:07:12,600 --> 00:07:19,090
And then at runtime they can each perform their own area calculation and assign it into the area value.

114
00:07:19,140 --> 00:07:23,310
And so it doesn't have to know beforehand what type of shape you're picking because you have the different

115
00:07:23,310 --> 00:07:24,480
implementation for it.

116
00:07:24,540 --> 00:07:29,390
Or you could say something like that and then be like oh that's a really good example.

117
00:07:29,850 --> 00:07:34,050
You know the same thing you know if you're building a videogame you know you might have a you might

118
00:07:34,050 --> 00:07:40,690
have a humanoid class and a humanoid class is just you know a base parent class and then the humanoid

119
00:07:40,700 --> 00:07:45,930
class might have Orcs because they're humanoid they got hands and feet they can speak to the Elves but

120
00:07:45,930 --> 00:07:50,900
you know elves are magical and mystical they can do their own things and orcs are brutish and you know

121
00:07:50,970 --> 00:07:51,980
ugly.

122
00:07:52,000 --> 00:07:53,890
So they've got to put an ugly modifier on there.

123
00:07:53,900 --> 00:07:57,930
And that's actually real that's a real example in games like World of Warcraft and other things.

124
00:07:57,970 --> 00:08:01,610
They're using polymorphism and inheritance all over the place.

125
00:08:01,710 --> 00:08:08,460
So that is polymorphism simple principle to understand once you do it and you're going to do it that's

126
00:08:08,460 --> 00:08:08,610
it.

127
00:08:08,610 --> 00:08:12,230
Mark Bryce had slopes dot com polymorphisms.
