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