1

00:00:01,130  -->  00:00:08,850
Not look at polymorphism it is very likely that you must have heard of book inheritance as well as polymorphism

2

00:00:08,970  -->  00:00:11,610
even before you enrolled in this course.

3

00:00:11,610  -->  00:00:18,390
We already a report inheritance and polymorphism is another awesome feature of object oriented programming

4

00:00:19,110  -->  00:00:21,430
and it exists Jadoo inheritance.

5

00:00:21,600  -->  00:00:23,270
That is because we have inheritance.

6

00:00:23,430  -->  00:00:26,640
We are able to take advantage of polymorphism.

7

00:00:26,640  -->  00:00:31,340
So let's look at what polymorphism is and what benefits we can have from it.

8

00:00:32,070  -->  00:00:35,840
Every class defines a contract through its methods.

9

00:00:36,390  -->  00:00:40,310
That is it and says that I have these kinds of methods.

10

00:00:40,310  -->  00:00:46,870
Now if the class is public and if it has public methods then the contract is the API.

11

00:00:47,670  -->  00:00:54,510
Now if class is a supertype then its contract is also defining a common protocol for all its subtypes

12

00:00:54,510  -->  00:00:55,700
.

13

00:00:56,070  -->  00:01:03,430
That is it is announcing that book myself and all my suppliers have these kinds of methods and Vira

14

00:01:03,450  -->  00:01:10,170
that inheritance permits this are subtypes will inherit all the methods defined in the supertype.

15

00:01:10,350  -->  00:01:16,950
Nobody finding such a common protocol for subtypes supercop is also providing the benefit of polymorphism

16

00:01:17,450  -->  00:01:25,500
and lets see what it means this polymorphism is supertype can be assigned any of its subtypes.

17

00:01:25,770  -->  00:01:30,960
For example lets consider this method abrade profile which Abdias the profile information of a user

18

00:01:30,960  -->  00:01:31,710
.

19

00:01:31,710  -->  00:01:34,830
So the method but ometer is of pipe user.

20

00:01:34,830  -->  00:01:42,330
So obviously we can pass a user object to this method but due to polymorphism we can pass an instance

21

00:01:42,540  -->  00:01:45,570
of any subclass of the User class.

22

00:01:45,630  -->  00:01:51,390
This is possible supertype is defining a common protocol for all its suppliers.

23

00:01:51,780  -->  00:01:55,620
That is anything you can do with a supertype object within this method.

24

00:01:55,680  -->  00:02:02,070
You can also do the same with any of the objects of subtypes that is within this method.

25

00:02:02,160  -->  00:02:07,980
If you are in working a method on the user object then that same method will still be invoked even if

26

00:02:07,980  -->  00:02:11,020
an object off subtype is passed.

27

00:02:11,040  -->  00:02:13,450
So what is the benefit of this.

28

00:02:13,860  -->  00:02:19,850
One is that code is much more flexible that its code is less susceptible to change.

29

00:02:19,920  -->  00:02:21,340
And what does that mean.

30

00:02:21,570  -->  00:02:25,720
For example user class can acquire new cell process in future.

31

00:02:26,340  -->  00:02:30,650
But this method of profile doesnt have to change at all.

32

00:02:30,720  -->  00:02:32,340
That is it would remain the same.

33

00:02:32,730  -->  00:02:40,580
So fewer changes imply fewer bucks and the court is also locked in are no better polymorphism.

34

00:02:40,640  -->  00:02:45,750
We would have maintained four different versions of a blade profile method for the four different types

35

00:02:45,750  -->  00:02:46,920
of users.

36

00:02:47,070  -->  00:02:52,410
That is we would have one update pro-helmet method with the user's parameter on one with staffer's parameter

37

00:02:52,460  -->  00:02:53,570
and so on.

38

00:02:53,790  -->  00:03:00,050
Also with every newly added user type and you update profile method would have to be created but now

39

00:03:00,060  -->  00:03:05,210
we have only a single market that is less good and also plinker.

40

00:03:05,320  -->  00:03:13,560
So here a user can pick any form hence this feature has been named as Polly morphism in this example

41

00:03:13,580  -->  00:03:13,920
.

42

00:03:13,950  -->  00:03:19,250
The parameter is it polymorphic reference that is how it is referred to us.

43

00:03:19,410  -->  00:03:28,020
However even the method return type can be polymorphic reference so we set up with polymorphism as supertype

44

00:03:28,020  -->  00:03:31,110
can be assigned any of its subtypes.

45

00:03:31,110  -->  00:03:37,920
It means that the type of object reference and the actual object referenced can be different.

46

00:03:37,920  -->  00:03:44,760
In this case the type of object reference has to be a supertype of the actual object type that is being

47

00:03:44,760  -->  00:03:48,430
referenced in this example declaration.

48

00:03:48,460  -->  00:03:54,720
The reference type on the left is user while the actual object referenced is editor and we know that

49

00:03:54,740  -->  00:03:58,740
user is supertype of editor before polymorphism.

50

00:03:58,740  -->  00:04:04,010
All the declarations we Lubet have same type on either side of the assignment.

51

00:04:05,010  -->  00:04:08,230
Now that reference type an object type can be different.

52

00:04:08,390  -->  00:04:12,700
There is something important you need to know about method invocation.

53

00:04:13,500  -->  00:04:20,520
Compiler uses a reference pipe to decide on vedette a method can be invoked on an object reference or

54

00:04:20,520  -->  00:04:21,210
not.

55

00:04:21,600  -->  00:04:27,030
So it doesn't use the object type but it uses a reference type to make this decision.

56

00:04:27,030  -->  00:04:32,550
Here in this declaration a reference type is user an object type is editor and we get a compiler error

57

00:04:32,970  -->  00:04:37,220
when we try to invoke a prove review method on the reference variable.

58

00:04:37,620  -->  00:04:45,060
That's because compiler knows that User class which is a reference type does not have this method but

59

00:04:45,060  -->  00:04:53,670
at runtime JVM uses the actual object type to decide on which method is invoke and GBM in books.

60

00:04:53,670  -->  00:05:00,240
The more specific question of the method in the inheritance tree starting from the object type and moving

61

00:05:00,270  -->  00:05:01,730
up words in the tree.

62

00:05:02,070  -->  00:05:09,690
Let's actually look at an example it as a class diagram from orchestrally let's have we have this declaration

63

00:05:09,780  -->  00:05:11,880
that we saw earlier.

64

00:05:11,880  -->  00:05:16,230
Next we are invoking post a review method on use that variable.

65

00:05:16,320  -->  00:05:18,260
Now there are two implementations of this method.

66

00:05:18,330  -->  00:05:24,780
One in User class and Bunyan's stuff and GBM would invoke the wash and stuff.

67

00:05:24,780  -->  00:05:31,330
That's because JVM starts searching for this method in the inheritance tree starting from Ed. onwards

68

00:05:31,340  -->  00:05:31,510
.

69

00:05:31,870  -->  00:05:34,620
As editor is the object type in the declaration.

70

00:05:35,160  -->  00:05:41,300
So as it comes up the inheritance tree from Ed. It will find the method and stuff across.

71

00:05:41,500  -->  00:05:49,470
So JVM tries to find the more specific portion of them at their next offensive blink but is book then

72

00:05:49,480  -->  00:05:50,430
GBM in groups.

73

00:05:50,500  -->  00:05:57,630
The only question in the User class knowledge to assume that Syb bubbling muttered internally in gawks

74

00:05:57,810  -->  00:05:59,590
post-Arab you matter.

75

00:05:59,880  -->  00:06:03,600
Then can you guess which version of the method would be invoked.

76

00:06:03,610  -->  00:06:04,360
Would it work.

77

00:06:04,380  -->  00:06:06,240
Posted in User class.

78

00:06:06,250  -->  00:06:09,600
A sibling is also in use of class.

79

00:06:09,630  -->  00:06:16,270
No it would still invoke the poster if you mentored in the staff class and that's because JVM once again

80

00:06:16,360  -->  00:06:24,150
starts exploding from Ed. class onwards as editor is the object type that is being referenced and that's

81

00:06:24,140  -->  00:06:25,690
about polymorphism.

82

00:06:25,690  -->  00:06:30,820
The main takeaway is that polymorphism allows us to write code that is flexible.

83

00:06:31,290  -->  00:06:34,670
That is code that is less susceptible to change.

84

00:06:34,790  -->  00:06:41,680
And the second benefit is it allows us to write less code and consequently the code is cleaner.

85

00:06:41,680  -->  00:06:47,550
Let me not give a very quick more of what we learned in this demo.

86

00:06:47,660  -->  00:06:50,350
People does do things that we have learnt.

87

00:06:50,510  -->  00:06:56,810
Customer does the main polymorphism feature that allows subclass objects to be assigned to a superclass

88

00:06:56,820  -->  00:06:58,300
reference.

89

00:06:58,290  -->  00:07:02,300
Next rule does the method in location concept to save some time.

90

00:07:02,500  -->  00:07:07,450
I'm sure in the demo in my Eclipse soon I'll use switchover to eclipse too.

91

00:07:08,010  -->  00:07:14,770
So here we have three classes use and or DOS dots or Java an editor or Java just like the way we have

92

00:07:14,760  -->  00:07:18,330
in our history and user Dajjal out.

93

00:07:18,370  -->  00:07:20,520
That is a user class is the superclass.

94

00:07:20,520  -->  00:07:25,880
We know that and all three classes reside in this package called semantics here.

95

00:07:25,890  -->  00:07:34,500
Oop staff do our job in order to extenso user iron ed. class expense staff and Witan user we have this

96

00:07:34,500  -->  00:07:36,350
method called Print user type.

97

00:07:36,420  -->  00:07:44,640
All it does is simply prints the word user and stuff also has the same method but it prints stuff editor

98

00:07:44,670  -->  00:07:47,810
also has the same method and it prints editor.

99

00:07:47,820  -->  00:07:53,100
Now there is a fourth class at testor Class B which is qualis user test.

100

00:07:53,130  -->  00:07:59,590
It also has the same method print user type which has a parameter user.

101

00:07:59,580  -->  00:08:07,180
So that's a polymorphic reference type and all it does is it invokes the method print user type on the

102

00:08:07,620  -->  00:08:10,480
power of the object that is being passed here.

103

00:08:10,980  -->  00:08:16,650
And this is the main method here and user is also in the same package.

104

00:08:16,690  -->  00:08:17,900
So here.

105

00:08:17,940  -->  00:08:24,870
Initially we are declaring to be able to use our staff an editor and the reference type is user for

106

00:08:24,880  -->  00:08:32,680
all of them and for the user variable the object typist user and for the stuff variable the object is

107

00:08:32,680  -->  00:08:35,850
stuff and for editor it is editor.

108

00:08:36,000  -->  00:08:43,930
Now we are first creating an instance of user test and then we're invoking its own limited print user

109

00:08:43,920  -->  00:08:44,480
type.

110

00:08:44,740  -->  00:08:51,390
And we are passing user object directly and then in this statement We are passing the staff object and

111

00:08:51,380  -->  00:08:53,940
then the third one we are passing the editor object.

112

00:08:53,980  -->  00:09:00,320
So we are passing three different objects and the reference type is just a user.

113

00:09:00,370  -->  00:09:04,870
So we discussed this we said that that's the benefit of polymorphism.

114

00:09:04,870  -->  00:09:05,190
Right.

115

00:09:05,190  -->  00:09:11,740
We can just how one matter and in future if we add any other objects like email or admin or chief editor

116

00:09:11,980  -->  00:09:12,720
we can.

117

00:09:12,930  -->  00:09:14,980
We don't have to make any changes.

118

00:09:15,070  -->  00:09:18,450
We don't have to change this method and we can simply use the same method.

119

00:09:18,690  -->  00:09:24,990
So awfully It gives us flexible code and it's also less that you don't have to maintain through different

120

00:09:25,000  -->  00:09:27,390
methods for each of the user objects.

121

00:09:27,390  -->  00:09:34,800
So let me just run this and it prints the user when the user object got passed when the stuff got passed

122

00:09:34,880  -->  00:09:41,370
a printed staff because it invoked the print user type method in the office staff object that got passed

123

00:09:41,390  -->  00:09:41,480
.

124

00:09:41,590  -->  00:09:43,520
And then for editor it entered it.

125

00:09:43,620  -->  00:09:47,340
So that's part one of our demo.

126

00:09:47,360  -->  00:09:51,590
Now let's do the part 2.

127

00:09:52,090  -->  00:09:59,380
So user or Java has these two amateur's save a blink and post a review on Silverlink.

128

00:09:59,380  -->  00:10:02,760
It just prints this message user Colonsay a blank.

129

00:10:03,000  -->  00:10:05,860
And it is also in working the Post review method.

130

00:10:06,070  -->  00:10:13,090
Unposted if you mattered also we have this text that's getting printed user it just means that we are

131

00:10:13,090  -->  00:10:19,900
inside user class and then we are printing the name of the method unstaffed or Java has just the Post

132

00:10:19,900  -->  00:10:20,670
review matter.

133

00:10:20,670  -->  00:10:27,490
Just like in the case study and editor just has this empty matter approve review.

134

00:10:27,630  -->  00:10:34,830
And so let's just go back to user test and let's just consider this declaration here.

135

00:10:35,390  -->  00:10:38,590
And because it helps us get past the method location.

136

00:10:38,590  -->  00:10:41,480
So let me just say editor dot.

137

00:10:41,620  -->  00:10:48,150
Now you can see this the benefit of using an ID it gives this job done with all the methods that the

138

00:10:48,150  -->  00:10:49,990
user class has.

139

00:10:50,460  -->  00:10:59,860
So let me just try to prove review of is inside the editor but compiler says that there is no appropriate

140

00:10:59,940  -->  00:11:00,510
method.

141

00:11:00,580  -->  00:11:04,360
That's because compiler is looking at the reference type.

142

00:11:04,360  -->  00:11:04,650
Right.

143

00:11:04,650  -->  00:11:05,770
We discuss this.

144

00:11:05,970  -->  00:11:07,780
So it's not going to look at the object right.

145

00:11:07,810  -->  00:11:09,330
So this is not going to work.

146

00:11:09,580  -->  00:11:12,120
So let me just comment this out.

147

00:11:12,660  -->  00:11:14,700
Now let's call Ed..

148

00:11:14,740  -->  00:11:23,430
Dr. Boorstein review and let me just comment these three out.

149

00:11:23,850  -->  00:11:24,520
OK.

150

00:11:24,580  -->  00:11:27,580
Let me just your on this.

151

00:11:27,580  -->  00:11:32,650
Now it says staff posted if you're supposed to review in the staff class has been invoked.

152

00:11:33,000  -->  00:11:39,420
So JVM is actually searching the inheritance tree starting from Ed..

153

00:11:39,490  -->  00:11:45,280
It is going up words the words user class and it could find the method and stuff even though there was

154

00:11:45,630  -->  00:11:47,770
the same method and user.

155

00:11:48,070  -->  00:11:50,820
So that's supposed to review next.

156

00:11:51,100  -->  00:11:53,160
Let me comment this out.

157

00:11:53,160  -->  00:11:59,980
So the more specific method was accessed called Save of a blink.

158

00:12:00,720  -->  00:12:07,630
And from this now it's just invoked saber rattling in the User class.

159

00:12:07,870  -->  00:12:15,250
So that got blended and we know that see a weblink is in Durnan rocking Post review and a post review

160

00:12:15,250  -->  00:12:22,510
it did not invoke the one end user but it invoked the run and stuff we already discussed this and we

161

00:12:22,500  -->  00:12:30,460
said that once again because we are GBM is going to start its search from Editor because editor is the

162

00:12:30,450  -->  00:12:31,990
object type.

163

00:12:32,380  -->  00:12:34,920
And that's about it that's about polymorphism
