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