1
00:00:06,050 --> 00:00:06,800
Hello, Jim.

2
00:00:06,830 --> 00:00:10,960
So they are going to answer questions, what are generics in Java and how they work?

3
00:00:11,270 --> 00:00:14,540
We'll start from understanding of why do we need generics.

4
00:00:14,810 --> 00:00:19,070
After that, we'll understand how you can parametrized your mascots and classes.

5
00:00:19,340 --> 00:00:22,610
You're going to understand what Boundy generics in Java are.

6
00:00:23,030 --> 00:00:27,110
We'll discuss generics with multiple mounts also in this class.

7
00:00:27,110 --> 00:00:30,870
And we are going to learn what the wild cards are and how to use them.

8
00:00:31,310 --> 00:00:35,510
Today, we're going to have a lot of practice that will help you to understand generics.

9
00:00:35,840 --> 00:00:41,510
And at the end of the lesson, I will explain what a type is and what type erasure is.

10
00:00:41,900 --> 00:00:45,480
Let's start and to make you understand what generics are.

11
00:00:45,650 --> 00:00:50,580
I suggested starting from examples to make you understand what problem generics intended to solve.

12
00:00:51,200 --> 00:00:52,760
Imagine the next iteration.

13
00:00:52,790 --> 00:00:57,170
You have array of strings and you want to have mastered that process is data.

14
00:00:57,350 --> 00:01:02,240
For the sake of example, let's imagine that you need to have masses that would print all elements of

15
00:01:02,240 --> 00:01:04,430
array to console in some custom way.

16
00:01:04,820 --> 00:01:11,600
Here I created massive print array that takes array of strings as Masad argument inside a grade for

17
00:01:11,600 --> 00:01:14,270
each loop and before printing any new element.

18
00:01:14,450 --> 00:01:19,040
I also print words aliment and only after that some element is printed.

19
00:01:19,580 --> 00:01:25,040
Now you realized that you have not only array of strings in your program, you also want to apply the

20
00:01:25,040 --> 00:01:27,080
same behavior for an array of integers.

21
00:01:27,410 --> 00:01:32,900
But the existing method will not work with an array of integers since it is specified that it works

22
00:01:32,900 --> 00:01:34,190
only with the array of strings.

23
00:01:34,580 --> 00:01:38,690
Let me uncommon this line and you can see a compilation error here.

24
00:01:39,110 --> 00:01:44,750
What to do in this case, create another method that will work with array of integers, I assure.

25
00:01:45,140 --> 00:01:50,360
What would you do in case you would need the same method for array of numbers or array of your custom

26
00:01:50,360 --> 00:01:51,690
type user, for example?

27
00:01:52,190 --> 00:01:54,710
That is definitely not the best option.

28
00:01:54,950 --> 00:02:00,050
But what I would recommend to do in this case, it would be perfect to create the method that would

29
00:02:00,050 --> 00:02:03,290
work with arrays of different types, what they think.

30
00:02:03,800 --> 00:02:09,070
But how to do this in Java, that is exactly what generic mechanism is created for.

31
00:02:09,560 --> 00:02:14,960
In other words, we can create parameterized methods and classes that would work with different types.

32
00:02:15,320 --> 00:02:21,350
And here you already can see the first advantage of using generics you can avoid of code duplication

33
00:02:21,350 --> 00:02:23,750
and increase cautery usage in your program.

34
00:02:24,290 --> 00:02:27,980
Let me comment this parametrized method and explain use syntax.

35
00:02:28,550 --> 00:02:34,760
This method is similar to the one we already have, but before the type of returns value, we have just

36
00:02:34,760 --> 00:02:35,360
word here.

37
00:02:35,570 --> 00:02:38,180
I write it in Diamond Operator.

38
00:02:38,810 --> 00:02:45,170
These characters from both sides are called together as diamond operator whispered Sify, our parameterized

39
00:02:45,170 --> 00:02:46,940
type in the diamond operator.

40
00:02:47,330 --> 00:02:52,940
This is also called generic and will explain in simple words what this is about.

41
00:02:53,520 --> 00:02:55,910
This is a placeholder for some specific type.

42
00:02:56,210 --> 00:02:58,750
Theoretically, I can write Annunziata here.

43
00:02:59,120 --> 00:03:05,870
The main thing is that letter that I specified here will be the same as in the Massett parameters and

44
00:03:05,870 --> 00:03:06,580
Method Masmoudi.

45
00:03:07,190 --> 00:03:15,110
And after I wrote it here, I can treat this E as a specific type and use it with parameters, for example,

46
00:03:15,260 --> 00:03:17,030
or anywhere inside the method.

47
00:03:17,060 --> 00:03:23,810
But here you can see that I have array of type E and while iterating over each element in this area,

48
00:03:23,960 --> 00:03:26,830
I also use E type for my variable here.

49
00:03:27,320 --> 00:03:34,100
Now I can call this method for most string and integer array the same as for all other types.

50
00:03:34,490 --> 00:03:40,490
Let me comment, Prenter, a message that works only with strings and leave my generic method awesome.

51
00:03:40,850 --> 00:03:45,070
And you can see that I don't have any compilation error anymore here.

52
00:03:45,650 --> 00:03:48,050
Let's make a definition of generic method now.

53
00:03:48,530 --> 00:03:52,880
Generic method is a method that introduce its own type parameters.

54
00:03:53,520 --> 00:03:57,310
I believe now you understand what generics are and why do we need them.

55
00:03:57,920 --> 00:04:04,220
Also, you know how to parametrized your methods now, but what is a potential issue in this case?

56
00:04:04,490 --> 00:04:07,520
In this case, for the type inside the method body?

57
00:04:07,550 --> 00:04:13,640
I can use only Massett that are available for object type, but not any other methods that are specific

58
00:04:13,640 --> 00:04:21,470
to string or integer or any other type Y because compiler doesn't know what type will be here during

59
00:04:21,470 --> 00:04:22,220
the runtime.

60
00:04:22,610 --> 00:04:29,540
I can't call here any string methods because compiler can guarantee that only objects of type streambed

61
00:04:29,540 --> 00:04:30,320
will be here.

62
00:04:31,010 --> 00:04:37,250
As we can see on this example, objects of any type might be here and what to do in case I need some

63
00:04:37,250 --> 00:04:40,760
behavior that is related to some specific hierarchy of objects.

64
00:04:41,150 --> 00:04:45,440
What if I know that this method wouldn't work with any objects at all?

65
00:04:45,680 --> 00:04:50,540
But this method has to work with any type that implements comparable interface.

66
00:04:50,990 --> 00:04:57,710
In this case, I can count on the fact that objects inside my body will have compared to Masset and

67
00:04:57,710 --> 00:05:00,830
I will be able to use this API in the mess and body.

68
00:05:01,100 --> 00:05:01,940
Does it make sense?

69
00:05:02,850 --> 00:05:08,850
To help you understand this, let me open another example here, I opened another file where I prepared

70
00:05:08,850 --> 00:05:10,220
other examples for you.

71
00:05:10,770 --> 00:05:16,710
Let's imagine that I have to create a that would be able to find the greatest value out of three variables.

72
00:05:16,980 --> 00:05:23,220
And I want to implement generic method, but not a bunch of separate methods that works with only strings,

73
00:05:23,340 --> 00:05:25,680
only integers, only double values.

74
00:05:26,010 --> 00:05:30,310
How to do that generic with boundaries will help us with this task.

75
00:05:30,910 --> 00:05:33,620
They're also called bounded type parameters.

76
00:05:34,170 --> 00:05:36,270
Here you can see massive declaration.

77
00:05:36,420 --> 00:05:38,550
The methods name is max value.

78
00:05:38,940 --> 00:05:45,450
The next thing that you can notice here is that we use different letter here now instead of E, we have

79
00:05:45,450 --> 00:05:51,120
key and he is parameterization of my method here in Diamond Operator.

80
00:05:51,120 --> 00:05:58,620
I say that this method should work with some type that extends comparable and comparable in turn is

81
00:05:58,620 --> 00:06:00,360
parameterized with this type.

82
00:06:00,990 --> 00:06:08,220
In other words, some type T should use it extends comparable interface or implement comparable interface.

83
00:06:08,790 --> 00:06:14,130
This is called upper bound with parameterization because we set boundary from the top.

84
00:06:14,460 --> 00:06:15,380
Doesn't make sense.

85
00:06:16,050 --> 00:06:17,970
Also, one more common here.

86
00:06:18,150 --> 00:06:23,070
It is possible to make multiple sounds like it is specified here in command line.

87
00:06:23,490 --> 00:06:28,530
I will leave it here, command it so that you could learn this when you will download the source code

88
00:06:28,530 --> 00:06:30,030
that I am showing right now.

89
00:06:30,630 --> 00:06:37,260
So you can say that we should extend both comparable interface and compare the interface.

90
00:06:37,800 --> 00:06:39,450
That's just for the sake of example.

91
00:06:39,960 --> 00:06:46,350
I believe you understand that instead of comparator, you can specify any other type here and only in

92
00:06:46,350 --> 00:06:50,790
case our custom type T will implement both of these interfaces.

93
00:06:51,000 --> 00:06:57,420
Only in this case we could work with this method and methods, arguments of type T, you even can see

94
00:06:57,420 --> 00:07:03,060
that I have a compilation error because neither string or integer nor verbal implements both of these

95
00:07:03,060 --> 00:07:04,560
interfaces simultaneously.

96
00:07:04,890 --> 00:07:11,370
Let me erase this to proceed with our demo potentially that you can also use time that is specified

97
00:07:11,370 --> 00:07:17,880
for parameterization as a returned type so you can get the type that was used as parameters here.

98
00:07:18,420 --> 00:07:24,630
The main thing and all of this is that this type should be the same everywhere across the MassArt in

99
00:07:24,630 --> 00:07:26,490
case the first argument is string.

100
00:07:26,610 --> 00:07:32,130
It is not possible that other arguments wouldn't be of different type or return type would be different.

101
00:07:32,760 --> 00:07:37,470
And now Compiler is aware that this type will be compatible with comparable interface.

102
00:07:37,710 --> 00:07:43,200
That means that all variables that are passed as arguments to this method have compared to MassArt.

103
00:07:43,740 --> 00:07:49,530
That's why all logic that is written in this method is rely on the fact that we can use this method

104
00:07:50,100 --> 00:07:52,110
and there are no any compilation errors.

105
00:07:52,380 --> 00:07:53,250
That's great.

106
00:07:53,880 --> 00:07:59,670
And here you can see in my method that I can invoke max value method with different types of arguments.

107
00:07:59,790 --> 00:08:06,210
No matter is as this integer double or string type, let me run this program from the console output.

108
00:08:06,210 --> 00:08:09,410
We can conclude that our method works as expected.

109
00:08:10,080 --> 00:08:13,230
Now you know how the Parameterize method was bound.

110
00:08:13,470 --> 00:08:15,200
Hobbs's makes things clearer.

111
00:08:15,450 --> 00:08:18,240
Let's go to the next example and this example.

112
00:08:18,240 --> 00:08:21,810
I'm going to show you how you can parameterize your class here.

113
00:08:21,810 --> 00:08:25,260
You can see Diamont operator right next to my class name.

114
00:08:25,680 --> 00:08:29,580
That means I parametrized my class, for example.

115
00:08:29,730 --> 00:08:37,110
I can declare property of type key and Masset parameters like in this set method also can be of my type,

116
00:08:37,110 --> 00:08:42,750
e.g. I can initialize my field with the values that will be passed as massas argument here.

117
00:08:43,530 --> 00:08:48,210
The method will return me a value of type that we used to parametrized my class.

118
00:08:48,750 --> 00:08:49,920
And here's example.

119
00:08:50,340 --> 00:08:52,170
I declare the variable of type generic.

120
00:08:52,320 --> 00:08:58,950
Mazrui was a name integer in box and used Diament operator to parameterize this type by integer.

121
00:08:59,430 --> 00:09:05,520
After that I created the object and used Diament operator after Anjelah version seven.

122
00:09:05,520 --> 00:09:11,970
There is no need to duplicate parameterization type in Diament operator here, but you can if you wish.

123
00:09:12,390 --> 00:09:18,810
For example, here I left integer type and then my second variable was a name string box that I parametrized

124
00:09:18,810 --> 00:09:19,440
by string.

125
00:09:19,680 --> 00:09:22,140
I left blank my second Diament operator.

126
00:09:22,590 --> 00:09:26,670
In most cases type is specified only when you declare a variable.

127
00:09:27,330 --> 00:09:33,120
And here you can see that my integer box can work only with integers and there is a compilation error

128
00:09:33,120 --> 00:09:35,400
in case I would try to set any string.

129
00:09:35,400 --> 00:09:38,700
Object is a similar story with my string box.

130
00:09:39,090 --> 00:09:44,700
That is because these two variables are parametrized with different types and in the first case set

131
00:09:44,700 --> 00:09:46,710
Masset except on the integers.

132
00:09:46,950 --> 00:09:50,580
And in the second case, set method accepts only strings.

133
00:09:51,030 --> 00:09:55,560
When I use get MassArt and receive integer and string type accordingly.

134
00:09:55,920 --> 00:10:01,890
And here you can see that I print values to console and used special format specifiers for that some.

135
00:10:02,040 --> 00:10:08,880
Numbers and string accordingly, let me run this program, you can see that everything works as expected

136
00:10:08,880 --> 00:10:09,950
without any errors.

137
00:10:10,440 --> 00:10:12,700
Let's create a definition of generic type.

138
00:10:12,720 --> 00:10:19,470
Now, when you saw the example and generic type is a generic class or interface that is parameterized

139
00:10:19,470 --> 00:10:20,160
over types.

140
00:10:20,920 --> 00:10:22,950
And now let's continue our lesson.

141
00:10:23,610 --> 00:10:28,920
Even if you would not use parameterization and generics for your own custom classes, you would use

142
00:10:28,920 --> 00:10:31,950
generics a lot with classes from Java collections framework.

143
00:10:32,190 --> 00:10:38,020
It is super useful because it wasn't possible to create collections for all possible types.

144
00:10:38,460 --> 00:10:43,710
That's why all types from Java collections framework are parametrized and use generics.

145
00:10:44,280 --> 00:10:46,410
Let me open source code of arriviste.

146
00:10:46,800 --> 00:10:54,240
You can see that it is parameterized with each type and if I open at Masset, you can see that it adds

147
00:10:54,240 --> 00:10:55,560
elements of each type.

148
00:10:59,000 --> 00:11:06,560
And get maced in return elements of each type, does it make sense sometimes even parametrized with

149
00:11:06,560 --> 00:11:12,510
two types, for example, on implementations specify separate type for key and value.

150
00:11:12,890 --> 00:11:15,530
Let me open hash map or squad here.

151
00:11:15,530 --> 00:11:23,210
You can see that I can specify type for key and a separate type for Lélia just to placeholders and input

152
00:11:23,210 --> 00:11:23,710
MassArt.

153
00:11:23,710 --> 00:11:31,220
You can see that I put key of type K and value of type we and type of return value is the same as type

154
00:11:31,220 --> 00:11:37,550
of the value we, because according to this interface we receive the last value that is associated with

155
00:11:37,550 --> 00:11:38,230
a given key.

156
00:11:38,750 --> 00:11:44,130
But the details of the interfaces of our collections framework are covered in the separate lessons.

157
00:11:44,810 --> 00:11:50,590
Now you know how to parameterize your class and you know how types from Java collections framework are

158
00:11:50,600 --> 00:11:51,430
parametrized.

159
00:11:52,130 --> 00:11:55,270
We have one more important and practical thing to talk about.

160
00:11:55,610 --> 00:11:59,840
I want to talk with you about the wild cards with upper and lower bounds.

161
00:12:00,310 --> 00:12:03,680
Let me open another demo file that I prepared before this lesson.

162
00:12:04,400 --> 00:12:09,970
To make you understand this example, let me show you all classes that will take part in our demo.

163
00:12:10,460 --> 00:12:16,970
I declared all classes in one file to keep everything grouped for the demo and to not switch tabs and

164
00:12:16,970 --> 00:12:17,720
engines that we have.

165
00:12:17,720 --> 00:12:22,850
Some parent class and child class extends parent and grandchild.

166
00:12:22,850 --> 00:12:24,260
Class extends child.

167
00:12:24,650 --> 00:12:25,360
Is it clear?

168
00:12:26,000 --> 00:12:26,420
Cool.

169
00:12:26,570 --> 00:12:27,310
Let's move on.

170
00:12:28,070 --> 00:12:34,040
Now imagine that I have at least parametrized by type child and I have mastered that can work with any

171
00:12:34,040 --> 00:12:37,100
list objects that are parametrized by time parent.

172
00:12:37,470 --> 00:12:39,650
It is called process parent elements.

173
00:12:40,010 --> 00:12:45,620
How do you think MacColl process span's elements MassArt and POS collection of child elements there?

174
00:12:46,310 --> 00:12:53,120
Logically, it seems to be OK since each child class should have all methods that parent class has and

175
00:12:53,120 --> 00:12:55,400
compilers should not be worried about this.

176
00:12:55,850 --> 00:13:00,260
But if I ask you this question, you already suspected something, don't you?

177
00:13:00,860 --> 00:13:01,490
You are right.

178
00:13:01,490 --> 00:13:03,830
If you are suspicious, this wouldn't work.

179
00:13:04,100 --> 00:13:05,990
You can see compilation error here.

180
00:13:06,110 --> 00:13:06,650
Why?

181
00:13:07,130 --> 00:13:11,120
Because list of child elements is not a list of parent elements.

182
00:13:11,330 --> 00:13:14,120
That said, what what to do in this case.

183
00:13:14,600 --> 00:13:17,630
Usually in this case, engineers use wild cards.

184
00:13:17,990 --> 00:13:23,900
Let me show you the next method we have process elements, method, the text list of elements that is

185
00:13:23,900 --> 00:13:26,840
parametrized by any type that extends Berent.

186
00:13:27,590 --> 00:13:31,850
This question mark is called wildcard and can be treated as any type.

187
00:13:32,330 --> 00:13:34,670
This is called upper bound at wild card.

188
00:13:35,090 --> 00:13:40,280
In this case, I can pass list that contains elements of type child to this method.

189
00:13:40,730 --> 00:13:43,160
And here you can see that there are no errors.

190
00:13:43,430 --> 00:13:44,360
Does it make sense?

191
00:13:45,170 --> 00:13:48,470
Now I want you to put all the attention to the next example.

192
00:13:49,190 --> 00:13:54,260
When you use cards, that means you can get elements of type parent from this collection.

193
00:13:54,680 --> 00:14:00,650
I believe that is clear that you will not be able to extract element of type child from this list because

194
00:14:00,650 --> 00:14:05,600
there is no guarantee what type would be here instead of the question mark during the runtime.

195
00:14:06,170 --> 00:14:09,350
It can be child, but it also can be parent type.

196
00:14:09,350 --> 00:14:13,340
And that's why custom foreign object to child type would cause exception.

197
00:14:13,700 --> 00:14:19,310
And most likely, parent doesn't have all methods and behavior that is described in the child type.

198
00:14:19,520 --> 00:14:20,330
Is it clear?

199
00:14:21,080 --> 00:14:26,720
OK, we understood that we can get elements of type parent in case we use upper bound at wild card.

200
00:14:27,230 --> 00:14:29,750
But can we add elements to this content?

201
00:14:30,170 --> 00:14:31,970
Unfortunately we can't.

202
00:14:32,480 --> 00:14:39,620
And here the examples you can see that I can't add neither parent nor child nor grandchild objects I

203
00:14:39,620 --> 00:14:40,970
cannot only know value.

204
00:14:41,090 --> 00:14:48,440
You might be wondering why why I can't add object of parent type to collection of elements, parametrized

205
00:14:48,440 --> 00:14:49,310
by parent type.

206
00:14:49,940 --> 00:14:52,010
But let's look at this from another side.

207
00:14:52,580 --> 00:14:57,440
This is a very smart mechanism created in Java to save engineers from potential errors.

208
00:14:57,860 --> 00:15:04,640
The main issue is that because we don't know what type would be here during the runtime, that's why

209
00:15:04,640 --> 00:15:07,170
interactions with such collections are limited.

210
00:15:07,760 --> 00:15:13,940
Imagine that during the runtime I will pass list of elements of type child and I will add element of

211
00:15:13,940 --> 00:15:14,750
time parent.

212
00:15:15,140 --> 00:15:21,140
And after I modified this collection in this method, I will get the element and I would think that

213
00:15:21,140 --> 00:15:27,310
all elements in this container are of child type and I will call some child specific method.

214
00:15:27,590 --> 00:15:28,550
What will happen?

215
00:15:28,880 --> 00:15:36,620
I will get error during the runtime because indeed I will get here object of parent time and this object

216
00:15:36,620 --> 00:15:38,840
will have child specific method.

217
00:15:39,380 --> 00:15:46,310
That is why Compiler wants to eliminate cases like this and just doesn't allow me to add any elements

218
00:15:46,310 --> 00:15:47,120
in this method.

219
00:15:47,630 --> 00:15:52,850
Does it make sense and leave now you understood how upper bound at wild cards work.

220
00:15:53,330 --> 00:15:55,610
Now let's take a look at another case.

221
00:15:55,790 --> 00:15:58,130
What to do in case I have to create.

222
00:15:58,670 --> 00:16:04,790
That will be able to add new elements to the collection in this case, we have to use lower bound at

223
00:16:04,790 --> 00:16:08,810
wild cards here you can see that I created two more lists.

224
00:16:09,050 --> 00:16:14,420
One of them is parametrized by parent type and another one is parametrized by child type.

225
00:16:14,630 --> 00:16:20,630
And you can see that I call process elements to Masset and parse the first and the second list object

226
00:16:20,630 --> 00:16:20,990
there.

227
00:16:21,470 --> 00:16:25,820
But I can't pass list of elements that are parametrized by grandchild type.

228
00:16:26,150 --> 00:16:28,850
Let's investigate process elements to MassArt.

229
00:16:29,360 --> 00:16:31,790
This method has lower bound and wild card.

230
00:16:32,120 --> 00:16:34,190
You can see super keyword here.

231
00:16:34,550 --> 00:16:35,690
What does that mean?

232
00:16:36,200 --> 00:16:42,890
That means that this method can take as an argument any object of type liste parametrized by any type

233
00:16:42,890 --> 00:16:47,700
that is parent or in other words, super type with respect to child type.

234
00:16:48,330 --> 00:16:52,370
That's why I can't pass Barrentine or super type of child.

235
00:16:52,790 --> 00:16:57,470
This could be any super type, including parent and even object is clear.

236
00:16:58,190 --> 00:17:00,040
If yes, then let's proceed.

237
00:17:00,410 --> 00:17:05,930
In this case I can get on the elements of type object and I believe you already understood why.

238
00:17:06,290 --> 00:17:11,360
Because there is no guarantee what super type of child type will be here during the runtime.

239
00:17:11,900 --> 00:17:18,410
But what types I can put into this collection, I can put any time that this child with respect to child

240
00:17:18,410 --> 00:17:23,780
type here, you can see that I can add object of type child and grandchild.

241
00:17:24,200 --> 00:17:32,090
And also I always can add now, for example, I can't add any super type of type child, including parent

242
00:17:32,090 --> 00:17:38,660
type and object y if this sounds completely not logical, then consider the next case.

243
00:17:39,110 --> 00:17:45,320
Imagine that we have passed here container full of objects of type child and in case compiler let me

244
00:17:45,320 --> 00:17:46,460
add the parent object.

245
00:17:46,640 --> 00:17:52,460
And after this collection was modified, I retrieve object of child type from here and called child

246
00:17:52,460 --> 00:17:59,570
specific method and program fails because that object doesn't have child specific behavior and case

247
00:17:59,570 --> 00:18:05,300
I passed here at least parametrized by any other type that is super with respect to child type.

248
00:18:05,540 --> 00:18:12,500
There is no harm in this at all to add child or grandchild object in this container because each of

249
00:18:12,500 --> 00:18:16,430
the specific types for sure has behavior of its parent type.

250
00:18:16,820 --> 00:18:17,990
Does it make sense?

251
00:18:18,590 --> 00:18:24,350
We can make a conclusion in case you want to create a method that will work with collections, parametrized

252
00:18:24,350 --> 00:18:30,080
by different types, and you want to use this method to modify the collection and to add elements there

253
00:18:30,290 --> 00:18:32,600
you have to use lower bound at wild cards.

254
00:18:32,960 --> 00:18:38,390
But in case you wanted to use this method only to read elements from the container to process that data,

255
00:18:38,630 --> 00:18:45,020
then you have to use upper bound at wild cards because upper bound wild cards allow us to read from

256
00:18:45,020 --> 00:18:50,150
the container specific type and use its methods to get all necessary data.

257
00:18:50,960 --> 00:18:56,840
You already learned how to use bounded wild cards, but should we use wild cards without bounds?

258
00:18:57,260 --> 00:18:59,780
I would say that this is super rare case.

259
00:19:00,380 --> 00:19:00,980
That's why.

260
00:19:01,010 --> 00:19:02,720
Let me show you this on example.

261
00:19:03,200 --> 00:19:05,630
Let me open source code of absurd collection.

262
00:19:06,260 --> 00:19:09,920
You can see that absolute collection is parametrized by any type.

263
00:19:10,220 --> 00:19:11,990
But let's look at contains all.

264
00:19:11,990 --> 00:19:14,540
Macit, can you see wild card here.

265
00:19:15,020 --> 00:19:21,140
That means that contains some method can work with any collection that contains any types inside.

266
00:19:21,680 --> 00:19:24,700
Then what is the difference between each type and wild card?

267
00:19:25,160 --> 00:19:31,730
The point is that each type will be erased and substituted with a specific type everywhere with the

268
00:19:31,730 --> 00:19:38,150
type that we will specify, whereas wild cards would always mean any type, but not the exactly one

269
00:19:38,150 --> 00:19:41,480
type that we used to parameterize instance of our object.

270
00:19:41,660 --> 00:19:43,160
Does it make things clearer?

271
00:19:43,640 --> 00:19:48,620
I will leave linked to this example in attachments to this Larssen so that you could investigate it.

272
00:19:48,950 --> 00:19:55,730
But pay attention that this specific example you won't be able to run because I described various scenarios

273
00:19:55,730 --> 00:19:57,320
that causes compilation error.

274
00:19:57,860 --> 00:20:03,200
But still you can play around with compilation properties to understand wildcards together with upper

275
00:20:03,200 --> 00:20:04,190
and lower bounds.

276
00:20:04,800 --> 00:20:09,620
Let me open the next demo file to show you the next example and this example.

277
00:20:09,620 --> 00:20:13,670
I want you to understand the generic exist only for our compiler.

278
00:20:14,140 --> 00:20:18,890
There are no generics during the runtime because of the type erasure type.

279
00:20:18,890 --> 00:20:25,640
Erasure can be explained as a process of enforcing type constraints only at compile time and discarding

280
00:20:25,640 --> 00:20:27,920
the element type information at runtime.

281
00:20:28,580 --> 00:20:34,660
That means that during the runtime it can be that objects of another type will appear in collection.

282
00:20:35,030 --> 00:20:42,320
Let me show this example and give my comments as we go and create at least Werrimull and list is parametrized

283
00:20:42,320 --> 00:20:43,520
by integer type.

284
00:20:43,970 --> 00:20:49,760
Everything looks good so far, but now I created a list without specifying any generic type.

285
00:20:50,150 --> 00:20:57,080
That means that during the runtime, the EIB that is declared in the source code of list will be erased

286
00:20:57,470 --> 00:20:57,740
and.

287
00:20:57,970 --> 00:21:04,570
Aware where it was used, it will be substituted with object type, for example, at Mass, it will

288
00:21:04,570 --> 00:21:05,830
work with any object.

289
00:21:06,310 --> 00:21:13,030
But in case I use bouncin generics, like, for example, in our damos was a generic for our method

290
00:21:13,330 --> 00:21:14,680
during the compilation.

291
00:21:14,710 --> 00:21:21,300
This type will be erased and substituted with the first bound class, in this case comparable.

292
00:21:21,730 --> 00:21:22,740
Does it make sense?

293
00:21:23,500 --> 00:21:27,670
You can see that our compiler tells us that there is a one here.

294
00:21:28,120 --> 00:21:32,540
It is not an error, but still works of our attention in the opinion of compiler.

295
00:21:33,100 --> 00:21:36,390
If I am a mouse over this warning, I see explanation.

296
00:21:36,400 --> 00:21:39,340
Message that tells me is at least is a type.

297
00:21:39,880 --> 00:21:41,080
What is a raw type.

298
00:21:41,440 --> 00:21:45,010
A raw type is the name of a generic class or interface.

299
00:21:45,010 --> 00:21:46,710
Without any type arguments.

300
00:21:47,110 --> 00:21:49,030
We can still work with such types.

301
00:21:49,120 --> 00:21:52,960
But Compiler wants to warn us that something bad may happen.

302
00:21:53,500 --> 00:21:59,740
Now I Telx at least reference is pointing out to the same object as integers reference.

303
00:22:00,160 --> 00:22:02,590
And I put string object in that list.

304
00:22:02,890 --> 00:22:07,930
And again, I see he has a warning about time safety, but not an error.

305
00:22:08,410 --> 00:22:11,830
This is very unlikely that you will be able to reproduce this mistake.

306
00:22:12,100 --> 00:22:18,220
But I want to prove you that generics exist only to help us during the compilation time, but not during

307
00:22:18,220 --> 00:22:18,910
the runtime.

308
00:22:19,510 --> 00:22:25,510
And as you can see, I successfully edit my string object to the list, but when in other parts of the

309
00:22:25,510 --> 00:22:32,350
program or somewhere else will use the reference of our parametrized type integers and when will call

310
00:22:32,350 --> 00:22:38,500
that method, we will get object of type integer because this variable has parameterized type.

311
00:22:38,890 --> 00:22:40,990
But what would happen during the runtime?

312
00:22:41,470 --> 00:22:42,340
What do we think?

313
00:22:42,820 --> 00:22:44,980
Lets me around the program and show you this.

314
00:22:45,460 --> 00:22:49,000
And we see runtime exception, glossiest exception.

315
00:22:49,480 --> 00:22:54,810
Java tells us that in line 15 it can't costarring object to integer.

316
00:22:55,300 --> 00:22:56,710
I believe you got the trick.

317
00:22:57,310 --> 00:22:58,000
That's it.

318
00:22:58,390 --> 00:23:02,970
Now you know about even more than some senior software engineers, believe me.

319
00:23:03,580 --> 00:23:05,590
Let's recap what we have learned today.

320
00:23:06,220 --> 00:23:08,410
Today, we learned a lot of different things.

321
00:23:08,740 --> 00:23:13,390
Now you know what generics are and what potential problems they help us to solve.

322
00:23:13,840 --> 00:23:20,290
Youso how to parameterize your method in Java, we learned what Boundy generics are together.

323
00:23:20,290 --> 00:23:22,150
We created parameterized Closs.

324
00:23:22,630 --> 00:23:24,580
You saw what wild cards are.

325
00:23:24,700 --> 00:23:29,230
And we practice a bit with upper bound at wild cards and lower bound with wild cards.

326
00:23:29,620 --> 00:23:33,640
And at the end of the lesson, we talked about hyperalgesia during the runtime.

327
00:23:34,300 --> 00:23:36,370
And now let's look at your homework.

328
00:23:36,940 --> 00:23:42,940
I believe you already implemented our custom list from the other homework I left link to that homework

329
00:23:42,940 --> 00:23:44,230
here just in case.

330
00:23:44,710 --> 00:23:50,380
Also, I count on the fact that you implemented Iterator and listed the return for this container.

331
00:23:50,620 --> 00:23:53,350
Here is a link to the homework about iterators.

332
00:23:53,800 --> 00:24:00,100
Now, I want you to make my list Interface Parametrized, and I want you to use generics for the fourth

333
00:24:00,370 --> 00:24:06,760
E class implementation myside that I want you to make here in your classes that implements Iterator

334
00:24:06,760 --> 00:24:08,680
and least iterator interfaces.

335
00:24:08,710 --> 00:24:09,940
Also parametrized.

336
00:24:10,630 --> 00:24:15,930
This will be a great practice for you to work with generics that it home.

337
00:24:15,960 --> 00:24:17,980
This task will be like a piece of cake for you.

338
00:24:18,730 --> 00:24:20,560
That's all what I have for you for today.

339
00:24:21,010 --> 00:24:22,520
Thanks a lot for your attention.

340
00:24:22,810 --> 00:24:24,460
See you in the next lesson.
