1

00:00:02,160  -->  00:00:04,470
Hi there this is they don't want to lose you.

2

00:00:04,470  -->  00:00:05,600
And welcome back.

3

00:00:05,940  -->  00:00:08,340
Earlier we looked at unbonded Walcott's.

4

00:00:08,340  -->  00:00:10,730
Now let's look at bonded Walcott's.

5

00:00:11,040  -->  00:00:16,320
In this lesson will look at the motivation for bondable cuts and then the follow up lesson we will get

6

00:00:16,320  -->  00:00:18,240
into the details.

7

00:00:18,240  -->  00:00:24,420
Now if you're developing a library for your own organization or let's say for public in general then

8

00:00:24,420  -->  00:00:31,920
using of brony cuts can make your API very flexible and uming that your model parameters deal with generic

9

00:00:31,920  -->  00:00:33,210
types.

10

00:00:33,210  -->  00:00:35,990
You can notice this in the method from the utility class.

11

00:00:36,030  -->  00:00:41,760
Joe Lockhart you can deduct collections just note that this whole topic of wild cards on generic methods

12

00:00:41,790  -->  00:00:47,490
can get pretty confusing but a lot of effort has been put into explaining these topics in a very intuitive

13

00:00:47,490  -->  00:00:47,980
way.

14

00:00:48,480  -->  00:00:53,280
But still you would have to do quite a bit of brainstorming on your own and to really internalize these

15

00:00:53,280  -->  00:00:54,540
concepts.

16

00:00:54,720  -->  00:00:58,380
So nothing to be worried if you're having heart Bangert in these concepts.

17

00:00:58,530  -->  00:01:04,230
They're pretty simple if you get a good conceptual understanding of these topics which is what we aim

18

00:01:04,230  -->  00:01:05,260
to do here.

19

00:01:05,790  -->  00:01:12,100
But some extra effort unpracticed might be needed from your and also not the terminology wise.

20

00:01:12,120  -->  00:01:19,140
We can also refer to Behrami Christ types that use wild cards as simply white card types just makes

21

00:01:19,140  -->  00:01:21,490
it more clear.

22

00:01:21,850  -->  00:01:26,510
We know that genetics is invariant which helps with compile time type system.

23

00:01:26,640  -->  00:01:33,870
That is if book is a type of bookmark then list of book is not a subtype off list of bookmark just a

24

00:01:33,880  -->  00:01:35,070
fish or a memory.

25

00:01:35,070  -->  00:01:38,850
This was the example we looked at while discussing invariance.

26

00:01:39,300  -->  00:01:41,470
If genetics are not invariant.

27

00:01:41,640  -->  00:01:47,820
The list of book can be passed as argument here and within the method movie object can be added to it

28

00:01:47,830  -->  00:01:48,300
.

29

00:01:48,780  -->  00:01:54,370
We know that this can very likely result in a class Guste exception at runtime in the calling environment

30

00:01:54,900  -->  00:01:59,000
as it would be expecting the list to contain only objects.

31

00:01:59,210  -->  00:02:00,930
So type safety is broken.

32

00:02:01,070  -->  00:02:05,140
Why did generics use this invariant property.

33

00:02:05,670  -->  00:02:09,600
The invariant property also helps with type safety at compile time.

34

00:02:09,810  -->  00:02:15,370
It is also on a strict built in certain harmless scenarios where type safety is not a concern.

35

00:02:15,420  -->  00:02:20,820
For instance we may have a method called display that picks a list of bookmarks and simply displays

36

00:02:20,820  -->  00:02:21,630
them.

37

00:02:21,720  -->  00:02:24,560
The method is not going to add anything to the list.

38

00:02:24,900  -->  00:02:30,930
So you can pass a list of book are not worried that a movie object will be added to the list type safety

39

00:02:30,930  -->  00:02:32,170
is not a concern.

40

00:02:32,170  -->  00:02:34,760
And so the method is completely harmless.

41

00:02:35,100  -->  00:02:36,590
Hello digital invariance.

42

00:02:36,660  -->  00:02:41,880
We know that we cannot pass a list of book auto industry off movie as the metric parameter.

43

00:02:41,880  -->  00:02:45,230
Here is NStar bookmark.

44

00:02:45,300  -->  00:02:50,500
So one thing to do would be is to maintain separate methods for each of the parameter types.

45

00:02:50,980  -->  00:02:51,420
Hello.

46

00:02:51,420  -->  00:02:57,300
This will not compile as this matter would be considered as duplicates and that's because we know that

47

00:02:57,300  -->  00:03:03,390
at runtime data type it is for all genetic Soulik and notations will be addressed.

48

00:03:03,390  -->  00:03:10,050
So point to note here is that with generics such kind of method overloading will not work even if it

49

00:03:10,050  -->  00:03:15,870
was possible it would be very bad as we will have duplicate code which we know leads to a maintenance

50

00:03:15,870  -->  00:03:17,150
nightmare.

51

00:03:17,970  -->  00:03:20,070
So there are two solutions for this.

52

00:03:20,100  -->  00:03:24,540
One is to use an upper bound wild card which we will discuss in the next lesson.

53

00:03:24,630  -->  00:03:28,870
Basically the wild card here extends bookmark which acts as a bong.

54

00:03:29,190  -->  00:03:36,240
And with this we can invoke the display method with a list of bookmark or it list of any subtype of

55

00:03:36,240  -->  00:03:41,220
bookmark like list of book or a list of more the artist of the eyeblink.

56

00:03:41,960  -->  00:03:49,270
So not only do is to use a genetic method that bonded by parameter unbid this also we can with this

57

00:03:49,380  -->  00:03:54,760
primitive midlist of bookmark our last book or list of movie orderless tolerably.

58

00:03:55,140  -->  00:04:02,940
So book approaches are workarounds for invariance the are code much more flexible by allowing additional

59

00:04:02,940  -->  00:04:06,480
types of arguments to be passed us input.

60

00:04:06,480  -->  00:04:12,510
However Additionally they also ensure that compile time type safety is not compromised as we will see

61

00:04:12,510  -->  00:04:13,920
later.

62

00:04:13,920  -->  00:04:17,820
So you might wonder why not just use one of them all the time.

63

00:04:18,180  -->  00:04:19,370
That is all together.

64

00:04:19,400  -->  00:04:23,860
Avoid using get parametrized type like list off bookmark.

65

00:04:23,910  -->  00:04:29,580
The answer is there are certain situations where you would want to use these approaches unanswered in

66

00:04:29,580  -->  00:04:30,450
other situations.

67

00:04:30,450  -->  00:04:34,670
You cannot use them and that we will look at in the next lesson.

68

00:04:34,830  -->  00:04:39,450
Also at the end of this lesson there will be a demo of the second approach.

69

00:04:40,720  -->  00:04:45,500
Similarly let's look at another kind of restriction which is also due to invariants.

70

00:04:45,520  -->  00:04:51,190
Let's see if you have this method agregate which has a list of bookmark some not a parameter and the

71

00:04:51,190  -->  00:04:58,270
method can add things like bookmark book and movie objects to the input list for this discussion let's

72

00:04:58,270  -->  00:05:04,480
just assume that bookmark is not an abstract class and hence can be instantiated unordered as shown

73

00:05:04,480  -->  00:05:11,290
here period in rooking the matter with an other list of bookmark on the under there simply aggregates

74

00:05:11,710  -->  00:05:17,080
by adding different objects as you can see there is no type safety concern here.

75

00:05:17,140  -->  00:05:24,220
As you can pass only a list of bookmark you do invariance property nové may also want to invoke this

76

00:05:24,220  -->  00:05:31,570
method with a list of object that object is the supertype of book back on the bookmark book and movie

77

00:05:31,570  -->  00:05:33,990
objects can still be added here.

78

00:05:34,280  -->  00:05:35,580
How it'll do that in millions.

79

00:05:35,700  -->  00:05:41,820
Even this will not be possible as a list of objects cannot be assigned to a list or bookmark.

80

00:05:42,250  -->  00:05:47,040
It is not on the list off some type of book Mont. cannot be passed.

81

00:05:47,170  -->  00:05:51,560
But even a list of supertype of book bookmarks cannot be passed.

82

00:05:51,640  -->  00:05:54,010
It's got to be exact match.

83

00:05:54,090  -->  00:05:59,470
You cannot even use an upper bounded by the cart for the matter barometer here as it applies to on the

84

00:05:59,540  -->  00:06:03,140
bookmark on which subtypes.

85

00:06:03,190  -->  00:06:06,940
So the solution here is to use a lot more nearby.

86

00:06:07,540  -->  00:06:11,680
And here the key word super is used instead of excellence.

87

00:06:11,690  -->  00:06:18,490
Under this we could not invoke the method with either a list of bookmark or a list of object.

88

00:06:18,520  -->  00:06:22,050
We will also look at Lord Bernard Willcocks in the next lesson.

89

00:06:22,330  -->  00:06:28,510
No let's go ahead and do a quick demo of how the genetic method with Bond a type parameter can be used

90

00:06:28,510  -->  00:06:31,130
as an invariance book on clay.

91

00:06:31,150  -->  00:06:33,360
And it also ensures compile time type safety.

92

00:06:33,670  -->  00:06:41,840
So let's head over to equites OK here we are in the genetics class and a new method called invariants

93

00:06:41,840  -->  00:06:47,750
work on has been added and it's a generic method and it uses it bounded by parameter.

94

00:06:47,770  -->  00:06:52,220
So it has been appropriately named as in where it's worked on in the lecture we call it does display

95

00:06:52,750  -->  00:06:57,160
and this method takes a motet parameter single method parameter list of D.

96

00:06:57,190  -->  00:07:01,780
So here is the type parameter and we know that the extents number.

97

00:07:01,780  -->  00:07:09,520
So because of that we cannot pass either a list of number or list of some subtype of number like lowest

98

00:07:09,600  -->  00:07:13,040
of integer or a list of double or a list of a lot and so on.

99

00:07:13,320  -->  00:07:13,760
OK.

100

00:07:13,900  -->  00:07:19,690
So it just gives us more flexibility and it is a look at on the invariant property.

101

00:07:20,080  -->  00:07:25,570
It is use for harmless scenarios where types if B is not a concern or in the lecture we say that we

102

00:07:25,570  -->  00:07:30,910
just want to display some items in the list which is a harmless scenario that is we are not adding anything

103

00:07:30,910  -->  00:07:32,290
to the list.

104

00:07:32,290  -->  00:07:35,990
Now let's go back to the calling context which is in the main matter.

105

00:07:36,370  -->  00:07:40,430
So here it is here.

106

00:07:40,480  -->  00:07:42,040
So we're invoking them a third.

107

00:07:42,090  -->  00:07:45,040
Let me uncomment this.

108

00:07:45,190  -->  00:07:50,860
So here we are in working with an analyst of string and it is giving us a compilation error because

109

00:07:50,920  -->  00:07:55,030
we can only pass a list of number or a list of some type of number.

110

00:07:55,300  -->  00:07:56,930
OK so we can get past this.

111

00:07:57,010  -->  00:08:02,390
So let me comment that out here we are passing an analyst off number.

112

00:08:02,560  -->  00:08:02,860
OK.

113

00:08:02,860  -->  00:08:04,250
It's just perfectly fine.

114

00:08:04,360  -->  00:08:10,990
Or we can also has an integer that's also fine and we don't see any completion or.

115

00:08:11,230  -->  00:08:12,330
OK.

116

00:08:13,030  -->  00:08:15,310
Now let's get back into the matter.

117

00:08:15,940  -->  00:08:20,330
So we also said that it gives us compile time typesafe beat.

118

00:08:20,470  -->  00:08:24,970
So we should not be able to do something like a double.

119

00:08:25,300  -->  00:08:30,530
Because if we are to double then there is a chance for getting a runtime exception.

120

00:08:30,600  -->  00:08:36,250
Cost exception at runtime and we would also see that later for this at this time we get a compiler error

121

00:08:36,280  -->  00:08:36,390
.

122

00:08:36,560  -->  00:08:36,920
OK.

123

00:08:36,970  -->  00:08:45,040
Is that we cannot I guess you into the augmented because the ADD methhead uses this parameter type D

124

00:08:45,520  -->  00:08:48,190
which is nothing but this on the metric parameter.

125

00:08:48,190  -->  00:08:56,080
So because of this the add method is expecting the element that we are to be off by deep cannot be double

126

00:08:56,290  -->  00:09:00,650
and B as the boundary is a bounded type parameter here.

127

00:09:00,880  -->  00:09:08,660
So we can do that but we can we can actually do something like this so we can typecast it could be.

128

00:09:08,740  -->  00:09:13,070
So that element type is the here and we are typecasting the double B.

129

00:09:13,090  -->  00:09:17,530
And then we can order that is fine but normally you wouldn't do that.

130

00:09:17,700  -->  00:09:18,000
OK.

131

00:09:18,010  -->  00:09:18,880
Because.

132

00:09:19,150  -->  00:09:22,690
Because it can lead to an exception at runtime.

133

00:09:22,980  -->  00:09:26,790
We are compromising that typesafe be here by doing something like this.

134

00:09:27,070  -->  00:09:33,310
OK so if we go back into the into the into the IN WORKING called here.

135

00:09:33,310  -->  00:09:39,540
So let's say we have this list in dejour list of integer and we're passing it to the invariance we look

136

00:09:39,530  -->  00:09:42,710
at on matter and within the matter we are writing a double.

137

00:09:42,970  -->  00:09:48,640
So this is supposed to contain only only integers but in the method we are writing a double hand here

138

00:09:48,640  -->  00:09:54,780
we are fetching the zero at the element and the compiler will insert an implicit cast here.

139

00:09:54,800  -->  00:09:55,720
You know that.

140

00:09:55,720  -->  00:09:58,370
So here it is x we are expecting an integer.

141

00:09:58,480  -->  00:10:00,810
But what we have is a double value.

142

00:10:01,000  -->  00:10:08,050
So if we exit this so we get a crosscuts exception because the element is actually a double which has

143

00:10:08,050  -->  00:10:11,380
been inserted in the method and it cannot be cast to integer.

144

00:10:11,710  -->  00:10:17,910
OK so you normally would not do that but if I don't you have to do something like that.

145

00:10:17,980  -->  00:10:23,920
Then the typical way would be to pass the element here and then that is fine.

146

00:10:24,070  -->  00:10:31,750
Now it will not be a problem because in the invoking code we will be passing a list of integer and we

147

00:10:31,750  -->  00:10:37,290
would also be passing an integer here because we have that the type is the same type.

148

00:10:38,020  -->  00:10:39,880
So we can actually pass

149

00:10:42,520  -->  00:10:50,620
your test in this example we are passing the same list of integer but we we're also passing an angry

150

00:10:50,620  -->  00:10:51,250
value here.

151

00:10:51,250  -->  00:10:56,060
So this is perfectly fine and we won't have any issues when we run it.

152

00:10:56,530  -->  00:11:01,310
So if at all we want to add something then we can do something like this.

153

00:11:01,330  -->  00:11:03,090
It's got to be the same.

154

00:11:03,730  -->  00:11:09,110
Otherwise we get a compiler compiler error like we got here or we may get a runtime exception.

155

00:11:09,610  -->  00:11:14,380
OK so that's the invariants work and the second approach on the first approach was to use a wildcard

156

00:11:14,420  -->  00:11:14,690
.

157

00:11:14,960  -->  00:11:19,000
I'm not a proponent wildcard and we can see that we will see that in the next lesson.

158

00:11:19,240  -->  00:11:19,670
Thank you.

159

00:11:19,670  -->  00:11:22,030
And the code is available in the resources section.

160

00:11:22,090  -->  00:11:23,410
Happy coding.
