So we have seen how method writing is implemented. 2
And let's not forget what cannot be overridden. 3
And there are three things that cannot be overridden on the first one is a final method. 4
I that there is simply not my limited but it is declared the. 5
Key word final in its declaration. 6
We looked at final variables which meant that if variable's value once initialized can never be changed 7
that is the variable true value is final. 8
Similarly if finden method behavior is also final so cannot be overridden. 9
You will do this if you have written some logic. 10
You want it to work only in the way you have implemented it. 11
That is not of the subprocess should ever be able to redefine it next. 12
Fields cannot be altered and that is buttstock as well as instance variables cannot be overridden. 13
If you recall a discussion on method or writing we said that an instance variable in a superclass can 14
be hidden by an instance variable in a subclass by really declaring that we reachable in the subclass 15
. 16
That is a really clear variable. 17
They will have the same name as in the superclass. 18
On other things like variables Deedat I do not matter. 19
Only thing is they should have the same name. 20
Not a good question to ask would be why are instance variables not overridden by then instance methods 21
can be overridden. 22
Why did the language designers do that. 23
My best guess is that here we are talking about two different entities. 24
One is data that there is behavior. 25
Now when it comes to behavior that is the methods it makes sense to pick the more specific question 26
of the behavior with respect to the object in context. 27
An GBM is just doing it. 28
That's because inheritance is mainly about having classes of it more specialized logic. 29
So you would execute a specialized writing matter. 30
I would assume semantically same does not hold hard data that is Duffys. 31
What I would value the current be has in the current class is good enough on practically That would 32
be the most probable scenario. 33
Dutch studies and why language designers would have gone with this kind of behavior. 34
So no need to go down the hierarchy and become more specific you. 35
So that's the reason why. 36
Instance variables are also early bonder but not that incased a subclass object. 37
Instead of re declaring simply resigns will be a different value then we will see that value as there 38
is only one motion of the variable next tactic that cannot be overridden in the rest of the lesson. 39
Let's see why static methods cannot be overridden and the same argument would also hold forth static 40
variables. 41
At the end of the lesson we will do more of static methods not being overridden and in the next lesson 42
we will do a quick demo all priest not being or what he can do. 43
Let's consider it our familiar inheritance tree and I made minor changes here for the sake of this discussion 44
in the User class. 45
Same we see a book methods have been removed and a new method called static method has been introduced 46
in book user as well as stuff process on both methods are static methods. 47
Now here we have this declaration where we are creating an instance of editor under referenced by Peary's 48
user. 49
We are going to invoking Silverlink method which would invoke the same blink in the User class Nona's 50
as you say blink done in books posted of your method and this gives vino that posted Abele method. 51
In the staff class would be invoked as opposed to post-edit view. 52
End User class and we know that it's because JVM searches for the most specific words shown off posted 53
a view method starting from edit class as the object here is editor. 54
Let's also assume that save blank also invoke static method. 55
In this case the static method in User class would be invoked not the notion in the staff class a static 56
method are not overridden so static method in the same class is being invoked. 57
So here the class user is saying that I'm defining this method call Silverlink And if this method accesses 58
an instance method then I'm not going to get involved as I don't deal with state. 59
So the instance method to be invoked will be decided by JVM at runtime based on the object in context 60
bugger this good exercise one of my starting my turret's then it means that I want to access the static 61
method that is defined in my class as startlement that belong to me. 62
I don't get if the subclass object on which this method is invoked also has a similar static method 63
. 64
So I'm E-Class and if my code uses my class methods then my intention is to use my class matters not 65
somebody else's down the class hierarchy. 66
So that's a nice way of looking at this behavior. 67
But from a one method binding lesson we also know that stocktake matters are bonded compile time itself 68
and so the invocation of the static method is bownde at compile time itself. 69
So that's another way to look at it later in the day. 70
We will actually see this in action to get some more clarity. 71
Let's also look at an example where support classes have stopped instance methods that identical names 72
here. 73
How close super with one static method call it on one instance method called be and there are three 74
subclasses extending the superclass and they all have the same methods that are in the superclass not 75
Ajume duck. 76
We have this method foo class parameter. 77
S is of type super which is the superclass inside the method we are invoking both methods E and B on 78
the input reference variable S then method is invoked. 79
It will always invoke the static method in the superclass regardless of the object that gets assigned 80
to the input variable S that is you can invoke the method for any of the three subclass objects or even 81
the superclass object on only the method in superclass gets in. 82
As we discussed earlier the reason for this is there is a static method on stocktake but dirt's are 83
bounded at compile time based on the referenced type. 84
So the compiler precisely knows which method is invoked on blocks a static method in any of the subclasses 85
. 86
You just need to use the subclass named directly order reference type has to be offcuts Stop-Loss type 87
or you would need to apply a cast where the cast is off suppressed like Hollywood when it comes to in 88
Woking matter b. 89
And the second statement. 90
The method invoked the rarely depend on the object that got passed. 91
And we have seen this while discussing polymorphism. 92
So if an instance of subline is passed at runtime then each version of method B which is overriding 93
the motion in superclass is invoked if an instance of sapu is passed then its motion of method being 94
invoked. 95
But if super object is passed then the motion in super is invoked and we already know the reason for 96
this. 97
The method to be invoked will be decided at runtime based on the object type of the reference variable 98
. 99
We know that JVM invokes the more specific motion of the method. 100
So unless starting my third there is no one single word of method B that gets executed in the method 101
for. 102
It just depends on which object is passed. 103
Static method which is the method to be in go is finalized right away at compile time itself to reference 104
type. 105
And so what writing does not apply here. 106
The object if it comes into play at runtime is totally irrelevant. 107
Also note that just like in the case off topic an instance variables we said that he had this topic 108
matter in a subclass is hiding the static method in the superclass or not let's look at some more details 109
on syntactic methods. 110
And already this was mentioned earlier Superguy word is related to objects so it cannot be used inside 111
a static method. 112
If you have to access a hidden superclass method then you'll need to qualify the method name with the 113
super Krosnick unrealistic mollify. 114
It simply means superclass name should be followed by dog operator which is done followed by the mean 115
. 116
Next we know that static methods do not how and it can grow without it. 117
So just like the way a static method cannot access instance variables an instance method Dick cannot 118
hide instance methods and we get a compiler error if we try to do this. 119
Finally static methods cannot be overridden by instance methods. 120
And once again we get a compiler error. 121
If we tried to do this so that's a bog on why static methods are not overridden. 122
Nice thing is instead of simply accepting that starting methods are not overridden we kind of look at 123
why they are not overridden and that is more important non-North look at a very quick double on the 124
stuff that cannot be overridden. 125
OK let's look at final metrics. 126
We know that final methods can not be overridden. 127
So here we are and users are Java and this is a final method that we have created. 128
It's named as final method on your to an instance method. 129
Find an instance method on here you can see the keyword final which means that this is the final version 130
of the matter and should not be overridden. 131
Now start of July which is the subclass of user B have a new method here and instance method we just 132
call it has finally met method once again because we want to see if we can override it. 133
And now we get a compiler error. 134
It says cannot or I don't find one method from user so that you cannot write find other methods. 135
Now let's look at stocktake metrics. 136
Static methods cannot be ordered and they can only be hidden. 137
So here this is the method from the method binding demo and it is called a static method and we have 138
this print statement. 139
It's just printing the method name along with the class name. 140
So it's a static method now and stuff got Jawa. 141
Also I created a new static method and it has the exact same signature and it looks exactly the same 142
. 143
Only thing is the print statement says it is from stuff not in user test or Jawa. 144
So we have created this instance of editor and a reference type is user. 145
And here we are invoking the topic matter right. 146
So let me just run this. 147
So as you can see the work and end user is getting invoker. 148
That's because of early binding. 149
Right because static matter is not overridden and because of all because of early binding which we have 150
seen not method binding the notion in use it gets in Wilker because the reference type is what matters 151
here. 152
It looks at the reference type. 153
The compiler looks at the reference type and it decides that the motion and met user should be invoked 154
. 155
And that's because of the early binding. 156
OK now if you want to invoke the motion and static method then we can do this. 157
Basically we are applying a cast and the cast is a subclass which is a stop. 158
So let me run this. 159
Our knowledge system tactic method static methods can never be ordered and it can really be hidden. 160
So this is another example. 161
Here we are invoking the Silverlink method using the style reference ironed Silverlink as defined here 162
in the User class in the superclass and it simply prints undented in to post edit your method. 163
So this we have already seen and in one of the earlier demos. 164
So this would work. 165
The posted of your method in the subclass. 166
There is a posted of your method here which is an instance method on that instance method is already 167
written and in the staff class so there is a poster posted here. 168
So this is going to override that. 169
But the method is going to work this way even though it is getting invoked from sibling. 170
It's going to invoke this and that we know it's because of late binding because it uses the object type 171
so that we already know. 172
Then the interesting thing is here the static method is invoked one second the static method in the 173
user classes invoked. 174
But now it's not going to invoke the static method in the subclass. 175
It's only going to invoke the static method in the same class. 176
So this kind of illustrates that for days for posted if you mattered it's going to involve the motion 177
in the subclass. 178
And that's because of the object type. 179
But for this when it comes to start big matter it is going to invoke the Stop the method in the same 180
class not the one in the subclass. 181
So this kind of a monster is that. 182
So let's just go ahead and run this here. 183
So this is the first quarter save eyeblink and Silverlink is in the post-edit view and because of the 184
object type. 185
The one direction stuff is getting in. 186
But the Russian stuff is actually invoking the version and use it also so the user is getting in. 187
So that plus part of the demo that we hired for material were writing. 188
So that's for the instance my thoughts on when it came when it involved this topic matter it in the 189
wash and end user not the one and stuff. 190
So that kind of shows the difference between the two. 191
Soit was invoking the word topic matter in the same class etc.. 192
So that's about it. 193
That's about what cannot be ordered and