Hi there. 2
Welcome back. 3
This is Duminy loop. 4
Now we're going to do them all off the genetic markets and we are going to test out pretty much everything 5
that we have seen in the previous lesson. 6
So here we are in the genetics class under a new method called genetic math that the devil will has 7
been Ardit our Niccol been walk from the main matter. 8
In fact we are not going to run this method will mostly look at only compilation errors. 9
That's what we'll be doing. 10
So here we will look at many things like We will look at the type argument inference we are motard arguments 11
and also the type argument input and we are target type. 12
We will also look at explicit type arguments justification. 13
We will also look at generic constructor and we would also look at the aggregate mattered. 14
If you recall in the Wild Card them all there was an aggregate Metford which was not gambling properly 15
. 16
So Google fixed that and this using generic methods. 17
So we also look at that. 18
So let's get started now and let's look at this thing called S-type are our inference so everything 19
has been written and this particular method in the interest of time. 20
So you're not going to write any new code because it is locked or demonstrative here so here. 21
So this is the type argument type or inference. 22
So it is pretty similar to the method we saw earlier. 23
So here it's a static method and it uses this type barometer deep under written byte is void. 24
But despite parameter D is also used here as a matter of experimentor type and we're just printing the 25
name of this object in this way. 26
Plus on Dot get name which give us the simple name. 27
Now let's go back. 28
So in this case a double is being passed. 29
OK. 30
And the. 31
Type argument that is inferred is from this from the method arguments. 32
So we are going to infer it does double. 33
So if I how know what hit my Eclipse editor is showing declination of the method which says that the 34
type argument invert is double on the same double is also being shown here as a moderate parameter. 35
So eclipse is doing that for us. 36
It is showing this nicely. 37
And then once the the code will be available in the resources section on everything all the comments 38
are included. 39
So you can also look at all the comments and you get a chance. 40
And in the second example we are passing a string Djala. 41
So that type in four is string. 42
Now here there is a very similar megger it's named as type or inference. 43
Only one is added here. 44
So in addition to the methode here in the in addition to the method parameter the type parameter is 45
also used as the return type. 46
So we are simply returning object. 47
Earlier it was wired. 48
Other than that there is no difference here. 49
So the pipe in foot. 50
Once again we are passing string. 51
So the type inferred is string. 52
Right but the return type is also the type barometer B. 53
Ok so the compiler knows that it is going to return a string. 54
But in this case we are assigning it to a double. 55
Right. 56
On the string cannot be converted to debug. 57
So compiler is is telling us that it is wrong assignment here so that this needs to be changed. 58
So that's the compile time type safety benefit we get by using a genetic method. 59
So this just demonstrates the compiler error. 60
Not in this case we are displaying the compiler error but it highlights the fact that we are going to 61
pass some wrong arguments. 62
Ok so here we are using a method called audit collection which takes an ID and also a collection and 63
copies all the elements of the Adinda collection. 64
OK that's all it does on Hensick as named as outer to collection. 65
So let's go ahead and look at this method. 66
So here is the byte information be on. 67
Here is the audio off of that pipe on here is a collection of the pipe. 68
On here we are iterating through the elements of the array our days. 69
And we are just adding that element as you can see here the pipe is deep and we're adding it the element 70
all here always that element. 71
We are adding it to the collection. 72
That's all we are doing and first we are passing on in digerati and we're passing a collection off in 73
major underbite. 74
If you'll see it just be right. 75
So it should be inferred ASC-P. 76
So if I over it it says in danger. 77
So this is the type argument that is inferred and it is an integer array a mixed collection off into 78
. 79
So once again it is inferred from the arguments here. 80
Now if I change your train so we get our completion at her right we get a compilation here because I 81
would not be in for it but because this is an integer and this is a string. 82
But we were supposed to how as single type single type needs to be inferred. 83
And that is not possible because this is integer and district but if I change this to number then it 84
will be inferred as a number. 85
And that's because between digit a number numbers the supertype most common supertype And in this case 86
this is in court. 87
We also discussed that in the lecture so set. 88
So that's a bug in coding from the method arguments but if method arguments are not there are not specified 89
then we know that it can also infer from the target type. 90
And this is what is demonstrated here. 91
OK so let's ignore this method name. 92
Huge matter. 93
But to say that we are inserting from target type. 94
So if you look at this method it has a target type it has a method return type is also B but the method 95
does not or does not use any method parameters. 96
So inference has to be from the target type. 97
OK. 98
So if you go back. 99
So if I hover over it it says that string is that type that is inferred. 100
Now here in this case you see here in this case it is integer that Ogger type is integer. 101
So B would be in for integer. 102
But the only thing is if I wanted to get a compilation error I will get a runtime exception because 103
this method is actually returning a string but the compiler is going to insert a cast here saying that 104
it is integer because that's what it inferred from the target. 105
OK so if I had run it probably this is the only time you run it. 106
So we get a classic exception because Djalma long line docstring which was found here to be returned 107
cannot be Casta Gelada or $9.9. 108
OK so the compiler is going to insert ghast like this internally into a bytecode Mogami. 109
So that's about that. 110
So here the target pipe is being used but sometimes this kind of a motard can be part of another method 111
invocation. 112
So here we have a target type in Wilker one which is a method and to this method as an argument we are 113
passing we are invoking this method. 114
So this method does not have any method parameters here so it has two method arguments so it needs to 115
. 116
It needs to infer but that is not target type 2. 117
So in this case it would it would infer from the method in Wookey shouldn't context. 118
And we will see what that is. 119
But that works on me from July 8 onwards are not going to work in all the motions before July 8. 120
So I guess this is just the same at but the method name and Smith do. 121
This ends with one. 122
So let's just look at what this method is. 123
So it takes it. 124
Be here. 125
This is the type under written by is a list of a. 126
And it does something here let's not worry about it. 127
But the return type is list offbeat. 128
OK. 129
Now if we go back if we look at this matter type target type in worker 1 it has a list off-Strip. 130
OK this is a primary Kreiss type gander just done. 131
Wired So the the method parameter list of string for this for on for this it is the return type is list 132
off. 133
So that list of B will be compared with the list of strik this one since this has a list of string as 134
the method parameter now. 135
So that acts like a target type and the compiler would infer the type argument of this method as B. 136
But here it says a list of object are night that should be some bug within the eclipse Marsh which is 137
the editor that I'm using because if it is a list of object and if this one is taking a list of string 138
then that the assignment is not right. 139
Right. 140
That's because of invariance. 141
So it's got to be a list of string only then it will work. 142
If it was a list of string if it was something other than a list of string it would have shown a compiler 143
error but it is not showing. 144
So it should be a bug in a eclipse marsh. 145
So that's what it is. 146
Now let me just go ahead and change this properties and let me change this to 1.7 here. 147
So it's not going to work anymore. 148
So yes you can see it as giving some compilation orders here. 149
OK. 150
So it's not going to work so let's change it back to one point eight. 151
OK. 152
So that particular feature of inputting that kind of inputting was only introduced in Java 8 because 153
go compilation error has gone on here it's just a similar thing and start off. 154
This method we are creating an instance of idlest and we have the diamond notation. 155
Now the diamond notation also uses the type argument here. 156
But if it is not there then it would have used the target type. 157
OK. 158
Which is something like. 159
Or at least all string equals Do you want a list off this one which we have been seeing in the in our 160
demos. 161
So that's not the case here because it is it is in-circuit it is added here in the method and location 162
. 163
So in this case also it works in the same way. 164
This one takes a list of string so a string will be inferred here for this for this year for the generic 165
type but unfortunately the same of the bug that we saw here also seems to be appearing here we are. 166
We seem to have the same problem. 167
OK. 168
So that's about it. 169
Now here we are in walking type argument inference from Target type 2. 170
It's just the same method but here we are using a different type target type in walk or run. 171
This is not a great typing marker do you get that. 172
So this one uses list of the early articles list all string on it also written setlist offbeat but Gazzetta 173
irrelevant. 174
Solist offbeat so here. 175
So this would be a list of the now. 176
In this case it's a list of B. 177
Right. 178
So that type of this has to be inferred fast. 179
OK but this is also not inferred. 180
OK. 181
There are two non inferred things here. 182
So in this guess sense since it cannot be inferred because its argument is also not there and that is 183
not archetype also for this method. 184
So here the compiler places in it as object. 185
OK if it cannot infer it will just keep it as object its what is happening here now here and this method 186
is just the same invocation. 187
But we have a target type here just off string. 188
So if I holler at it it says that fart does matter type dargah typing work or do or does in court as 189
strict. 190
So it would become a list of string and then this one also because it has a list of B as the return 191
type. 192
So it will be in for a string but for this eclipse is not showing any is any issue. 193
So it says that it is in for the string even for this. 194
So for both of them are just string even for this method we are passing Nautilus off Diamond notation 195
even for this its target type in walk or do. 196
And for this also it will be a list of object. 197
But in this statement We are using a target type on your list of string and we are having the new order 198
list off this. 199
So in this case it is going to infer as string. 200
What about the method as well as for our list. 201
And you can say as I know it is not strict only in this case because there is no way for us to infer 202
it is seeing saying list of objects. 203
OK so thats the thing. 204
Now thats more ahead. 205
And now this is another example of inferring the most specific supertype we saw this in the lecture 206
. 207
So this method has a beat and then return type is B and both the parameters are also using the type 208
. 209
So if you go back we are passing a string on another list here. 210
If I hower Serializable is being in what we saw that in the lecture and this is also serializable. 211
Now if it does something as like an integer then it is going to give us a combination error. 212
So it needs to be serializable here because the return type is D. 213
Now this is another example. 214
Same things similar kind of thing. 215
But we are passing out elist instance of list on an instance off a set here has said. 216
So the common supertype is abstract collection. 217
Implements collection. 218
So thats why it is going great is footing as abstract collection here. 219
So lets not worry about all this thing but it is basically abstract collection. 220
And here we are calling it as useless generic method. 221
Lets see why it does use you has. 222
So if you go here this is the type parameter but the return type is void and the type parameter that 223
is not a parameter here and the method parameter list. 224
So basically it is pretty much we are passing it explicitly but it will not be used in any way in the 225
client order right. 226
Thats the benefit of genetics. 227
Had it been if it was using editor type then it could have inserted a cast but thats not the case it 228
is what are doing it under the parameters are also not there. 229
So there is no scope of passing in arguments. 230
So its just a list by parameter. 231
So this is just to highlight the usefulness of parameters. 232
So thats what it is ironed. 233
Now let's ignore this for now. 234
Not look at generally constrictors. 235
So here is the genetics demo we are in that Benedix demo class. 236
So lets look at these go here. 237
So it uses this class level parameter b and this is the generics Demel generics constructor. 238
And just like generic methods we are using this extents D. 239
So it is easy for this I need is extending this class level by parameter and this is also used here 240
as the parameter type. 241
The thing is genetic constructors are very rare. 242
I really did not find any examples for us. 243
So I think we dont have to worry too much about it but the eldest class here it uses here are enlist 244
he is a class level type parameter but this is a bonded bonded wild card. 245
So it is using like this. 246
We still did not discuss it or discuss bonded wild cards later but it can be converted into a genetic 247
constrictor. 248
But still this is the right way to do it. 249
In this case so it does really relate to how genetic construct hurts. 250
But this is just for your information. 251
And this case. 252
So here it is. 253
So we have this we are instantiating genetics to them all. 254
So this will be the pipe argument for the genetics demo class. 255
OK. 256
Now coming to the genetics constructor it will be inferred from the type argument just like genetic 257
metrics. 258
So it looks like genetic methods on a genetic constrictors we're going to limit that. 259
So it is going to be for Howard what it is saying that that argument inferred from the constructor is 260
double whereas for girls it is number no it is fine because doubleness going ekster number is futile 261
. 262
If you remember if you recall the constructor had the extents B. 263
So B is number here which is a classloader type but done on E is double now in the next line here. 264
So this one has a diamond notation under Diamon notation also uses repipe argument. 265
OK so in this case this will be double here and the constructor also uses the type argument. 266
So the type argument is used by both this notation but that I'm a notation as well as this. 267
So it's like Hower says double here. 268
It also says W here. 269
Now here you are you're passing it explicitly you're passing double for the constructor but for the 270
Class B a passing number that is perfectly valid because double extents number. 271
So here everything is explicit. 272
So that's pretty nice. 273
So here you pass it this way. 274
This is how you explicitly specify right before Gosney. 275
You can also specified here just after the there need not be the space here. 276
This also looks. 277
Now here this is this I'm really not sure why this is so this is explicit but here this is supposed 278
to be implicit. 279
So I assumed that it would infer that automatically that this is a double. 280
But it is not doing it are not. 281
I wish it could have inferred better does not doing. 282
So I really do not know why it does look as if I know what it says explicit type arguments cannot be 283
used with a diamond notation in an allocation expression. 284
So this is an allocation expression. 285
So I don't know the reason for that. 286
I know I did not find any not Hondas and I searched for that particular exception on in Google. 287
I did not find any any anything about that. 288
So let's just leave it like that. 289
Now here is another example where the constructor for the constructor does inferring from here like 290
it should from the method argument. 291
But for this it is inferring from the target type because if it is first double it this is going to 292
be generics and I'm off number then we have the invariance issue. 293
So the compiler is smart enough to infer a number from this guy. 294
So that's the thing. 295
And here is the final thing. 296
This is from. 297
We saw this in the wild cards demo. 298
So we had one less. 299
OK. 300
Then it was called as invalid agregate and we were aggregating the elements from this list from the 301
second list into the list. 302
So that to the three lists are surpassed here the the elements from the first on the second list here 303
are inserted into this. 304
And we know that in aggregate in reality that was a matter and you can go back and look at that because 305
of me because we were using wild cards. 306
It did not allow us to art here it was giving a compassionate heart because because of using the wild 307
card game. 308
But now we can add them. 309
So that means that the common disorder I think it's this one. 310
So as you can see this is all good. 311
One two three and four. 312
It's working fine. 313
So that's all there is for that not only thing here. 314
As I commented on this one this one we are also explicitly passing type witness here stood a monster 315
. 316
But here it's genetics Demel. 317
It is supposed to be when you remove this you're just a monster tinga. 318
OK. 319
So in this case is. 320
So in this case this is all compilation. 321
Let's not worry about it. 322
It's just demonstrate. 323
I'm just a monster getting dark. 324
This pipe argument inference I'm passing on explicit here. 325
Preakness here. 326
That's all there is. 327
So if you look at Doc takes a beat on we're passing genetics demarche self. 328
So that's why here I am passing an instance off in an accident. 329
So that's all that is so then. 330
My thoughts are really useful for static utility classes. 331
If you have something really genetic reuseable algorithmic kind of methods which can work on any input 332
then you need to go for that. 333
And by inference is there. 334
So that's about EQ. 335
So it's really useful you need to favor genetic methods. 336
OK. 337
Thank you. 338
Arne the code is available in the resources section. 339
Happy coding.