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