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