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