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