Hi there now that we know why language designers came up with Benedix let's not get started with the 2
basics of genetics in this lesson. 3
They will look at the syntax and terminology involved with genetics and also look at few restrictions 4
associated with genetics in the Allison will lead them off genetics. 5
Basically we do have them all off what we learned in this lesson. 6
Let's begin by looking at genetic type a genetic type is simply a class or an interface that type but 7
amateur's defined in the declaration on here is the basic syntax. 8
So the class name is followed by something called type parameters which are camos operator are not enclosed 9
in less than greater than symbols and within the class. 10
These type parameters can be used as byte for instance variables by foot barometers on local variables 11
within methods are constructors. 12
And also ask method root types on here as an example from the previous lesson. 13
The class store uses a single byte but I'm a dirt beat. 14
And as you can see despite barometer is used just the type for the instance variable A as well as method 15
parameter in the center. 16
And also as a return type for the get. 17
So we look at for the generic type s. 18
Now let's see what a parameter is this. 19
And also understand some Gentex terminology here at the top. 20
We have the genetic type store and we know that B here is the type barometer and is also referred to 21
as a formal type barometer under the bottom. 22
We can also see that for instance this off store are created for instance is a store of string that 23
is a string is stored in the stored instance that a string got response to that pipe barometer. 24
Be in the store class. 25
The second instance is a store off date. 26
Third instance is a store off list of data that is store instance stores a list containing data objects 27
unfought is stored off Brooke. 28
You may recall that in collections framework we did something similar to create lists of string on a 29
list of integers and so on. 30
So listener interface is a generic type. 31
Now here the reference by store of string is referred to as it parametrized by. 32
So the whole thing store followed by string. 33
But then the less done greater than symbiotes is up permit race type. 34
OK. 35
So we have the genotype on we have the parametrized type. 36
OK so just keep that in mind on just the string is referred to as type argument. 37
Our actual barometer. 38
Now if you recall our introduction to them at lesson in the language basic series we had similar terminology 39
method parameters were also called as formal parameters. 40
Well arguments passed during method invocation were referred to as actual parameters so that it was 41
formal parameters are natural parameters. 42
And here it has formal parameters on actual type parameters. 43
Because we are dealing with types no one thing to note as in the first three instance creation statements 44
we have the right argument on either side of the assignment. 45
How they would in the last week meant we have the type argument on the left side and on the right we 46
how M.D less than greater than symbols. 47
And this is called Diamond notation and it works only from July 7 onwards. 48
In this case type is automatically inferred based on the types specified on the left side. 49
So it is a shorthand so soit would be elegant to use this notation. 50
Now let's look at the naming conventions for type parameters. 51
You should definitely use a single uppercase letters and we can also see some conventions here. 52
If you are writing some kind of a collection type class then you can use an E Where. 53
E is for element. 54
If you're writing a map then use gay for the unbe for value. 55
If a generic type is dealing with numbers then you can use an and if your agenda type is a non-color 56
action and is not about numbers then you can use it. 57
B B would be the common one as generally B. 58
Right. 59
Non collection classes. 60
Typically you would have only one type parameter but if you have more than one then you can use s u 61
and v for the second third and fourth types just like SUV. 62
I don't think he will ever go beyond forth on even if you do you can use some other character. 63
Generally you can use any legal Jawa identifier but stick to this convention and this slide shows how 64
a generic type can be subduct on the left. 65
We have a generic interface called container. 66
Right. 67
We have the genetic Glassdoor which we saw earlier. 68
But in this case it implements the generic interface container on here and in an offbeat in the class 69
declaration of store is basically parameterized type on we can create an instance of storing this it 70
reference type is the generic interface container all the interfaces we saw in collection's framework 71
API are also gentrify and you may recall that we weren't instantiating their implementations in this 72
way. 73
For instance you referenced type would be a list of something or not right. 74
We would have something like an analyst in the demo that we will do in the next lesson we will implement 75
this court and we will also look at a couple of other things around subtyping. 76
This example just shows a generic class with multiple type parameters. 77
If we assume that all store class works like a map then it would look something like this. 78
VB What how parameters can be which will be used in different places within the classes body here can 79
be are used just pipes for key and value variables and then also used as constructor parameters and 80
also as written by IPS in the methods get the and get value nowness look at couple of core restrictions 81
that are associated with genetics and later in the chapter. 82
They will also look at few more restrictions here the restriction is that the type argument cannot be 83
remedied. 84
In this example the type argument is and it would give us a compiler error. 85
Similarly another restriction is doc type barometer cannot be used in a static context. 86
For instance it cannot be used as a type for stocktake Williams. 87
Here we have a generic class called device and we can see that static variable device type has the right 88
barometer B as that. 89
And this is illegal. 90
Lets see why this is illegal. 91
Do I understand it. 92
Let's assume that it is legal to define a static variable with that type barometer. 93
Here we have these two statements where we are creating two instances of device in the first instance 94
that type argument is smartphone while in the second that type argument is bigger. 95
Another question we can have is what would be after both these statements are executed. 96
Would it be smartphone or would it be pager. 97
If we assume that it would be pager just because it was the last it meant to be executed then the assumptions 98
made by the developer who wrote the first statement might become invalid for example double up on who 99
wrote the false statement would expect the type to remain stocktake when he Bassa smartphone on some 100
other code of his word expect the same. 101
Now if it gets changed to a pager then it could result in some undesirable behavior. 102
And that is the reason type barometer is not allowed to be used in this way since we are talking about 103
stocktake context by parameter. 104
Obviously cannot be used in a static method or in a static initializer. 105
So thats about it for now. 106
And as I mentioned earlier we will look at fuel more restrictions later after covering some relevant 107
concepts. 108
Thank you. 109
Our next move is more of what we learned and this lesson