Not let's look at method or writing we brief Your discuss method or writing while introducing inheritance 2
and it was part of the final design of a work history which is reproduced here here posted a behemoth 3
third in the staff class is actually writing Posture Review method and end a. 4
That is the operating method must have the same name deposited if you met that in the User class. 5
Well posted have you but the review needs to be approved by an editor. 6
We know that but the posted of you mattered in the staff class does not need any approval. 7
And so the overriding behavior is not needing any approval. 8
So then a regular user review than the Bush and the user classes invoke. 9
But if any of the staff members post a review then the posted of you there in the Stav classes invoked 10
in their democracy Gram's which we are protesting polymorphous on. 11
We did use both user unstaffed classes on both hardballs state of New Method. 12
So we already implemented Metro are we. 13
But there are some rules associated with method overriding and we will learn those rules. 14
So method or what rating writing what we are doing is redefining the behavior off the method in the 15
superclass and it could mean few things one of them is defining new behavior. 16
That is the overriding method does something different which is specific to sapless And another thing 17
can be to extend the behavior of the superclass method. 18
That is you would do what you want the superclass method does. 19
And then you would do something more. 20
Finally method of writing can also be used to provide better implementation. 21
For example the superclass method might belong to some third party library and it may have been written 22
very efficiently. 23
Now since it is a library card you don't have any control over the source. 24
So you can write a new class with a brand new method that overrides this inefficiently read and method 25
. 26
So the numerator would how much more efficient implementation. 27
Not in order to superclass defines a contract through its methods and consequently define a common protocol 28
for all x subtypes. 29
So when you order an item a third in the superclass you are agreeing to fulfill the contract. 30
That is you're telling the outside world that the only one writing method looks exactly like the order 31
didn't matter matter or writing in words. 32
Couple of rules that are based on this notion of fulfilling the contract. 33
In addition to having the same letter name there are two rules. 34
What method are writing to work. 35
Let's see what those rules are rules is if there are any method parameters. 36
Then they must be seen. 37
In addition the return types must be comparable and the second rule is that the overriding method can 38
be less accessible. 39
Let's not look at both these rules in some more detail. 40
In the first rule the fusspot which says that the method parameters must be same is straightforward 41
as the overriding mother should look exactly like the old written method. 42
The second part says that the return types must be compatible which means that the return type in the 43
overriding method must be either the same written by as in the superclass method or must be a subclass 44
. 45
This baby abide by the contract as a subclass. 46
Very true. 47
Everything is superclusters as it inherits all the methods of the superclass. 48
But if the Met the parameters are the same under different types are incompatible. 49
Then we get a compiler error. 50
No coming to the second rule it says that the alternating mudguard can be less accessible. 51
It means that access level must be either same or friendlier. 52
For example you cannot operate a public method and make it private. 53
Doing so under a compiler. 54
So those are the two rules for operating a method. 55
Next let's look at something called Superguy work as an email just it can be used to access a superclass 56
method from a subprocess and is typically used for extending behavior defined and superclass. 57
If the method its superclass is not aware it and in the subclass then it can be either accessed directly 58
using the method name as we already know or we are the super cute but typically you would directly access 59
it using the simple matter only not using the Superguy word. 60
But if the method in superclass is overridden I would want to access that method. 61
Then you must use super keyword resort example posted a view we know is an overriding matter in the 62
stuff glass circle in riposted. 63
And the user class from within this method in the staff class we would have to use supercool. 64
Otherwise we would be invoking the same method in the staff class could end up in an infinite loop after 65
working superclass method. 66
You can see that we can have some additional work thus extending the behavior that is defined in the 67
superclass matter and that's how you typically extend behavior. 68
Not that it is not necessary to invoke an order it doesn't matter. 69
Only an overriding matter over it and method in the superclass can be invoked from any of the subclass 70
methods. 71
Now Superguy word is related to objects so it can be used inside a static method and you get a compiler 72
error if you try to do the supect you can also be used to access something called it free which belongs 73
to the superclass. 74
We say that a field and supercluster is hidden. 75
If the field with the same name is declared and the subclass not that it is only to be a name that matters 76
here that macros to hide are free. 77
Everything else like that it by access modifier started non-stock to create is in Meridian. 78
If discipline's does not use Superguy won't always use a field in its own class or not Bahujan for your 79
superclass. 80
So that's method overriding. 81
But we still have a few more concepts to cover before doing that. 82
Let's first review what we learn so far in court. 83
Let's begin with testing or writing rules. 84
After that people look at the super keyword for this demo. 85
We will continue using lower classes related to our case study that is use it or jalouse staff but Java 86
and so on as we are going to test the roots. 87
Let's actually begin with the second overarching goal but says that the writing method should not be 88
less accessible. 89
So this is standard Java and we have this method posted a you which is operating at the posted of your 90
method and user class. 91
So in the User class it has public access level. 92
So in this stuff class also it has public. 93
So let's just make it less accessible here. 94
It's supposed to be either same or more friendlier but not less restrictive. 95
So as you can see we get a compiler error. 96
Says cannot reduce the visibility of the inherited METAR from user. 97
So you cannot do that it has to be either same or more friendlier. 98
So that's the second overriding rule. 99
Now let's just try the first method of writing. 100
And in order to do that I actually created a new class called Review and the review classes basically 101
represents a review and it has two variables one is sort of your text which holds the text for the review 102
and the other one is approved with a review that has been posted has been approved by an editor or not 103
. 104
If it is a user if it is if the review was posted by user needs to be approved by editor but if it was 105
posted by a staff member it does not need any ill. 106
And we know that. 107
So let's just start. 108
So this is a review class and the review text is initialized why this constructor and that proved reliable 109
. 110
We have to get it on center so since it's proved that there is. 111
We know that it is an artist named as easy. 112
So we saw that in our Naming Conventions does it or simply set approved. 113
So it's going to set the rules. 114
So in use at our job let me just but let's modify this let's introduce a new parameter here let's call 115
it DOES IT review text and so on. 116
Let's create a new view object. 117
It's pass in the rebute text. 118
It's just a view looks just great in this review. 119
So the written type has to be modified. 120
Here to review. 121
Yes. 122
So now if your object is getting created and since this is the user it's getting since the user is posting 123
this. 124
So if you go here and that if your text gets passed here under a broom since it does not have any value 125
it gets a default or false and which means that it does not get approved. 126
And that's what we need for effort. 127
If they are Beuys posted by user. 128
So we'll just leave it like that and let's going to add staff. 129
So staff has posted have you on we want to override it. 130
So let's change this review next. 131
So now this method has the same parameters as the overridden matter. 132
But we said that this was a compiler error because the writing method has the same name on the same 133
parameter but incompatible types in the superclass it was a review. 134
And here it is hard wired. 135
So here if I hollowed audit the message says the return type is incompatible. 136
So it needs to be a compatible return so it needs to be either review or one of its subtypes. 137
So let me just change this review line. 138
Let's just make this written new review so that we don't have any compilation that are supposed to review 139
as now or writing the superclass method and the rules have been followed. 140
So that's about overriding rules. 141
Next let's does the super keyword search super key word can be used to extend the behavior. 142
That is you can work a superclass method from a subclass supposed to review. 143
Let's say for example from the user. 144
This is what Dyster class. 145
Let's say we are in Woking So we have created this type of object here. 146
So staff object is here and has a reference type of user and the object type is stuff. 147
And we want to call the poster of you mattered and it's just called posted of your mentor and pass an 148
empty string as a review. 149
So unstaffed or Java. 150
So the first thing that we need to do here is let's invoke the superclass masterclasses super Dawk austerity 151
view and let's pass the review text. 152
Has he said of your text. 153
What is whatever has been passed in this matter. 154
So we have this and all you need to do now is just written this so now it's going to call the superclass 155
method. 156
So if you see here you just comment something on the second site says stuff posted even a user posted 157
review. 158
So we are invoking post-edit of your method and first the one in the subclass gets invoked here and 159
the first the one in the subclause got to invoke and put in staff post a review and then we are calling 160
the superclass method posted a few hundred buttons. 161
The second statement when the Superquinn the poster reviewing the user classes has gone invoke. 162
And that would return a review object. 163
And since we are and the staff class we need to set the approved status to recruit because the one that 164
is written by a user class will have on a status of fall. 165
So let's just make it true. 166
So it looks more realistic now. 167
So that's it said if you did not use this key word then you get you end up in an infinite loop because 168
you're just invoking the same post-Arab you mentored and that will give an error called stack overflow 169
error. 170
It's not what we want it is but main thing is that you can see you can see that it's always staff boss 171
to review underdosed just invoking recursively and at some point it will throw an error called Esquire's 172
and guttered and Sohmer call stack overflow error which is part of the Gelada act like package. 173
So that's because we were. 174
We kind of got into an infinite loop. 175
So it has to be super. 176
So that's one thing. 177
And we also said that superclass cannot be used so perky word cannot be used in a static Minturn's sort 178
of just put a start here to hide its name or does static mattered and nurses super Daut. 179
Here it says cannot use super in a static context. 180
So we can't use that. 181
So that's gone on the last thing about superballs that can be used to invoke a hidden field. 182
I actually added a field here in user class called ID which is initialized to 1 and I'm hiding it here 183
in the subclass stuff here. 184
I'm really declaring it here an idea is to not have this medicore print ID which is printing ID Fosdick's 185
going to print ID directly by using them. 186
I mean just a simple name ID on the second statement uses the super key word and it's going to user 187
test. 188
Not just uncommon this where we are accessing the prent ID motard sort of mirror just on this. 189
So here it says for ID pins s2. 190
And then for superdog ID Princesa one. 191
So if I go into this material you can see that when we are accessing ID it's actually accessing the 192
ID in the same class but. 193
So this idea is actually hiding that Id read and the superclass. 194
But when I say if I want to access the hidden food then I have to use supercooled here and that would 195
access the ID free. 196
So thats about it. 197
So we have to start everything that we have learned so far and upset and once again I'm showing this 198
an eclipse just to save some time and that's.