1
00:00:00,210 --> 00:00:05,670
Hey friends I'm very happy and excited to start the journey so let's get you onboard.

2
00:00:05,700 --> 00:00:07,440
So what are our generics

3
00:00:11,720 --> 00:00:18,500
simply stating genetics are mechanism for type checking at compile time

4
00:00:26,520 --> 00:00:28,930
and genetics but introduced in Java 5.

5
00:00:29,100 --> 00:00:36,740
And why should we use a genetics in application that may get any combines them at a run right in the

6
00:00:36,750 --> 00:00:37,240
court.

7
00:00:37,260 --> 00:00:43,430
Or we may encounter exception of trunk time line when we get any compile them enter the compiler forces

8
00:00:43,440 --> 00:00:44,640
us to fix that error.

9
00:00:45,060 --> 00:00:52,290
But in case of runtime enter the compiler it says does not know that there is going to be any error

10
00:00:52,290 --> 00:01:00,300
in that code and it is difficult for us to find that event actually that we're getting that exception.

11
00:01:00,540 --> 00:01:06,390
And we have to debug the code to find out that there or at which line we're getting the exception.

12
00:01:06,390 --> 00:01:10,830
So it becomes very difficult to find out very how we are getting the exception.

13
00:01:11,100 --> 00:01:19,690
So by using genetics we can a whites situation in Greece all type by using genetics with Dan the compiler

14
00:01:19,690 --> 00:01:26,130
that this is the data type I'm going to use and if I do any mistake then raise a compile time matter.

15
00:01:26,170 --> 00:01:34,030
So this is one of the major advantage of genetic and the other advantage is by using genetic V can reuse

16
00:01:34,030 --> 00:01:34,440
the code.

17
00:01:34,480 --> 00:01:37,320
So the other advantage is called usability.

18
00:01:37,330 --> 00:01:43,750
Let's take an example let's create a class let's plus create a package in this a genetics project.

19
00:01:43,750 --> 00:01:45,500
So we'll create a package.

20
00:01:45,500 --> 00:01:47,560
Calm down basics.

21
00:01:47,650 --> 00:01:48,310
Strong

22
00:01:52,010 --> 00:01:53,720
dart genetics

23
00:01:56,780 --> 00:02:08,420
and head of create a class simply data so we have this class data and inside this data class are we're

24
00:02:08,420 --> 00:02:11,170
going to how we will have a private feel.

25
00:02:12,980 --> 00:02:20,610
Private shrink type data.

26
00:02:20,680 --> 00:02:27,260
Now let's create get those and set those for data and let's have constructor

27
00:02:36,050 --> 00:02:43,320
Soviet constructor and inside this construct let's fight and let's get the definition string data

28
00:02:47,130 --> 00:02:55,280
the data and finally the how do string implementation for this class.

29
00:02:56,730 --> 00:02:58,030
So heavily.

30
00:02:58,240 --> 00:02:58,800
Don

31
00:03:02,520 --> 00:03:05,660
our string data

32
00:03:09,420 --> 00:03:09,960
is

33
00:03:14,650 --> 00:03:23,740
data so data classes ready now let's create one more class to demonstrate the need for genetics.

34
00:03:23,870 --> 00:03:29,160
So let's create one class demo that mean

35
00:03:33,840 --> 00:03:34,760
inside this class.

36
00:03:34,760 --> 00:03:36,830
Let's create object of data glass

37
00:03:43,300 --> 00:03:46,950
new data and we have to pass the data inside it.

38
00:03:47,020 --> 00:03:49,070
And the type for that is strength.

39
00:03:49,330 --> 00:03:52,220
So we have to pass the data in string.

40
00:03:52,360 --> 00:04:01,510
So this is D 1.

41
00:04:01,520 --> 00:04:03,680
Now let's bring

42
00:04:07,160 --> 00:04:12,720
by invoking the two string and let's run this

43
00:04:21,270 --> 00:04:25,860
So data is debunked so everything is fine until now.

44
00:04:25,910 --> 00:04:34,550
But now suppose I want this data to be in Deja What do I do now.

45
00:04:34,550 --> 00:04:37,400
Should I replace the string as integer.

46
00:04:37,400 --> 00:04:45,490
Or then I will not be able to use the string or I would not have the data string.

47
00:04:46,000 --> 00:04:47,770
So what should I do.

48
00:04:47,770 --> 00:04:49,540
Should I play string as object.

49
00:04:49,540 --> 00:04:53,880
So let's do this because I want string as well as in digit objects.

50
00:04:53,950 --> 00:04:56,920
So I will replace this with object

51
00:05:03,310 --> 00:05:07,600
object so I replace it everywhere.

52
00:05:07,630 --> 00:05:14,530
Now inside this demo I can create one more object date.

53
00:05:14,590 --> 00:05:15,860
Daddy do.

54
00:05:16,060 --> 00:05:20,010
And this time I can pass in Deja do it

55
00:05:24,220 --> 00:05:25,200
right.

56
00:05:25,420 --> 00:05:31,170
And if I retrieve the data in data

57
00:05:33,670 --> 00:05:34,100
and

58
00:05:37,410 --> 00:05:41,810
Deven and dart get data.

59
00:05:41,830 --> 00:05:49,610
So as you can see that suppose I mistakenly crossed a Deven to end I actually wanted to cost.

60
00:05:49,730 --> 00:05:56,200
But the cost data back to end but I mistakenly I caused Deven to end

61
00:06:04,250 --> 00:06:08,220
dark get data OK.

62
00:06:12,730 --> 00:06:20,800
So as you can see combined it is not able to detect this mistake.

63
00:06:20,800 --> 00:06:21,790
Now let's run this

64
00:06:26,300 --> 00:06:29,480
so see what gift we got we got.

65
00:06:29,480 --> 00:06:31,050
Class cast exception.

66
00:06:31,070 --> 00:06:33,420
So this is not the solution.

67
00:06:33,590 --> 00:06:35,720
We want to go for right.

68
00:06:35,720 --> 00:06:43,920
So by replacing object object is not appropriate solution that we should go for now should I create

69
00:06:43,920 --> 00:06:51,180
one more class with the data field and having integer as its type yes we can do so.

70
00:06:51,180 --> 00:06:54,510
But next I want to have double as the data.

71
00:06:54,750 --> 00:07:01,400
So should I keep adding classes on answer a simple use genetics how to use genetics.

72
00:07:01,410 --> 00:07:09,490
Let's cut this as you can see this data class in this to make this data class as genetic type if you're

73
00:07:09,490 --> 00:07:18,660
going to use angler brackets to specify the type that meter so then how does had the formal type parameter

74
00:07:18,870 --> 00:07:25,260
d x as a place holder for the actual type that the middle provide.

75
00:07:25,260 --> 00:07:28,570
So we call this D a type that I do so this type.

76
00:07:28,600 --> 00:07:33,940
But I mean did provide a V for us to reuse the same order with any input right.

77
00:07:34,110 --> 00:07:42,450
So this D is a type parameter.

78
00:07:42,500 --> 00:07:51,230
Now I want the type of data to be anything so I will replace it with D.

79
00:07:52,820 --> 00:07:54,590
So now it can be anything

80
00:07:59,310 --> 00:07:59,550
so.

81
00:07:59,680 --> 00:08:04,250
And now this data it says is has become a genetic type.

82
00:08:04,260 --> 00:08:06,210
It's a genetic class.

83
00:08:06,270 --> 00:08:08,490
Now this data is genetic class.

84
00:08:08,490 --> 00:08:13,960
Now let's back to this demo class on this mean metal.

85
00:08:14,030 --> 00:08:21,980
Now let's create Batam address type references to this genetic type data but I'm a trice type of bottom

86
00:08:21,980 --> 00:08:29,000
address type is the barometer that provide information to the compiler that what type is it so that

87
00:08:29,030 --> 00:08:30,500
it can check the types.

88
00:08:30,500 --> 00:08:35,060
So that there's a data Deven I will say this is string

89
00:08:39,150 --> 00:08:42,500
and her so now

90
00:08:46,130 --> 00:08:48,230
the compiler know the type of the data.

91
00:08:48,260 --> 00:08:52,840
So we do not need to costed that compiler does this thing by itself.

92
00:08:52,860 --> 00:08:55,460
Now I can create objects open any data type.

93
00:08:55,460 --> 00:09:01,550
I don't need to write separate classes for that so I can have this as integer

94
00:09:06,640 --> 00:09:11,170
so integer is the bottom address type and add the right.

95
00:09:11,210 --> 00:09:18,100
You do not and you are not needed to specify the parameter based type but you have to specify this diamond.

96
00:09:18,100 --> 00:09:25,680
OPERATOR How what do you can also provide it with integer but this is not necessary to do

97
00:09:29,190 --> 00:09:39,150
so if and as you can see we are getting this error cannot cost string to end it knows what a type it

98
00:09:39,540 --> 00:09:43,020
it what what type it is off.

99
00:09:43,030 --> 00:09:51,760
So it is not letting us do can wanted to do wrong type and when I run this let's run this code let's

100
00:09:51,760 --> 00:09:52,870
bring data to

101
00:10:04,110 --> 00:10:06,830
and see so data is Dan

102
00:10:09,600 --> 00:10:18,030
one more important thing to keep in mind about genetics is that that this team mean anything they're

103
00:10:18,030 --> 00:10:27,310
using these as a genetic to create an instance of an object but we cannot do this if we have a static

104
00:10:27,310 --> 00:10:34,600
method or feel because genetic type parameters are only in school for an instance of the class that

105
00:10:34,600 --> 00:10:41,020
means instance method any instance fields of over class because static members and fields belong to

106
00:10:41,170 --> 00:10:43,570
each and every instance of the class.

107
00:10:43,630 --> 00:10:52,300
So if you had an instance of data just like here we are having one instance of string data as a string

108
00:10:52,330 --> 00:10:59,830
and data as integer so we have to Einsteins but a different data type then what exactly should the static

109
00:10:59,830 --> 00:11:03,670
method be using string or in Deja said Gondry decided.

110
00:11:03,670 --> 00:11:10,870
So this concept is only applicable for class instances hope you might have understood what's going on

111
00:11:10,870 --> 00:11:16,210
under the hood and it was a quick introduction about genetics.

112
00:11:16,210 --> 00:11:17,620
Hope you understood it.

113
00:11:17,770 --> 00:11:19,570
So now let's move to the next lecture.

