Hi there this is they don't want to look too before looking at genetic methods. 2
Let's look at this property called invariance we mentioned about it earlier too. 3
It's a very simple concept and it is responsible for ensuring compile time type safety. 4
So let's go ahead and look at what it is. 5
We know that the promise of genetics is typesafe be at compile time. 6
Right. 7
Invariance is a property that also helps with this. 8
For instance let's consider a familiar hierarchy where book is it some type of bookmark. 9
Hello. 10
With ingredients we have this that is a list of book is not a subtype off list stuff bookmark. 11
So even the book is it's a type of bookmark list of book is not a pipe off the stuff bookmark and similarly 12
go to invariance. 13
Start off book is also not a supply off store or bookmark. 14
What it means is you cannot assign an instance of store or book store or bookmark all the book and be 15
assigned to bookmark. 16
And this is for ensuring compile time types if be not an example let's see why invariance is needed 17
to ensure compile time types. 18
If here let's consider this method go whose parameter is an idealist or bookmark. 19
Now for the sake of this discussion let's assume that genetics does not how invariant property and so 20
do with that here we are able to invoke the motet go which has an ideal list of bookmark estimate that 21
parameter bit and idlest logbook. 22
So the method argument is a list of book while the meathook barometer is it is not a bookmark on VTOC 23
invariants that should be possible. 24
Now that double up one would assume that the input argument which is an analyst or book would all of 25
this storm need book instances right. 26
That's the promise of genetics. 27
But putting them at that. 28
Go a movie object can be added to the input ideas. 29
And this is possible as the input of a list is declared as an idealist not a bookmark. 30
And we know that movie is a subclass of bookmark kits or movie object can be added to the list. 31
So essentially what is happening is a movie object is being added to a list that must contain only books 32
. 33
So if this is allowed then in the calling code the moronic class caste exception at one time as a movie 34
cannot be trusted with a book it sort of the court is extracting an object from the artist. 35
Then there will be an implicit cast on that would generate a cost kust exception as a movie cannot be 36
strictly a book. 37
So compile time types if he is lost hence invariance properties needed for ensuring compile time type 38
CFT with invariants. 39
This would be a compilation error in the method invocation statement itself. 40
And in this particular example the method go would have to be in more than idlest off bookmark. 41
So we know that immediacy does not allow this. 42
However for the same type argument inheritance of genetic types or so if that type argument is bookmarked 43
then an idealist or bookmark can be assigned to a list of bookmark as idlest is a subtype awfullest 44
can be a little bit confusing. 45
But you should keep that in mind. 46
So JENNETT exciting variant. 47
However arrays are something called Koiari. 48
What it means is if book is a subtype of book maake then a book or B is a subtype off. 49
Bookmark it. 50
And because of that we can assign a book ID to a book marketing. 51
And here is the same example as before but now I'm at that parameter is a bookmark for. 52
And in the invocation meant a book or is being passed so the body must contain only book instances there 53
will not be any competition at first. 54
Google Corleone's property but within the matter a movie object may be added to the input. 55
No this would not give a compilation error but at runtime they would end up with an arrest or exception 56
. 57
The exception would be thrown if a wrong type of object is being stored and then not it. 58
This was also demonstrated in one of the demotes earlier in the chapter. 59
So at no point we have compassion error here. 60
We only have runtime exception which is not good with generics. 61
The issue would be highlighted at compile time itself. 62
So book arrays as well as generics and shirt type safety. 63
It is just that generics insured type safety at compile time itself while R is and sure it on the runtime 64
and we know that compile time type c of D is prefered over types if we done. 65
And you could get this item from Effective Java recommends using less or worse. 66
I guess that doesn't mean that arrays are not important. 67
They have their place but if you feel typesafe needs to be ensured at compile time then you should go 68
Burtless non-attorney want to eclipse for a very short demo. 69
OK here we are in the genetics demo class and we are going to test both invariants as well as covariance 70
. 71
So we have these methods go for invariance and another go for covariance. 72
OK so the methods have been named as Tcl. 73
So let's just look at invariants and it just accepts an astronomer. 74
OK I know it's an empty body and we're going to invoke it from here from the main method. 75
So here we are passing. 76
And I don't list off integer. 77
OK here it's a list off number but here it is a list of integer integer we know is a subtype of number 78
but are a list of integer will not be a subtype of list of number. 79
All right so that's in millions. 80
And so we get a compilation error here. 81
OK. 82
Because we are compromising type safety. 83
So that's it. 84
So there is nothing to test here. 85
Next test go method here. 86
It accepts an ID off numbers OK. 87
And here we are passing an array of integer. 88
So this should store only integers but within this method we are storing a double case so there is no 89
compression order but we get a runtime exception. 90
If you run it. 91
OK so but we know that integer is just a pipe of number so an integer adic and all is also a subtype 92
off number and or F number. 93
So that's covariance property. 94
So you filter on it as you can see it says here is or it's invoking another method here. 95
Let me just come on this out 96
artist uniters side gives an artist or exception right. 97
OK and that's what is happening here. 98
Because we are trying to store it wrong type of object here. 99
It's supposed to be an integer but we are starting a double. 100
So you are getting in out store. 101
Exception at runtime which is not good. 102
So the item from effective Java. 103
It recommends using lists over this so that we get compile time type safety advantage. 104
So that's about it. 105
Thank you. 106
The code is available in the resource section. 107
Happy coding