Hey friends I'm very happy and excited to start the journey so let's get you onboard.
So what are our generics
simply stating genetics are mechanism for type checking at compile time
and genetics but introduced in Java 5.
And why should we use a genetics in application that may get any combines them at a run right in the
court.
Or we may encounter exception of trunk time line when we get any compile them enter the compiler forces
us to fix that error.
But in case of runtime enter the compiler it says does not know that there is going to be any error
in that code and it is difficult for us to find that event actually that we're getting that exception.
And we have to debug the code to find out that there or at which line we're getting the exception.
So it becomes very difficult to find out very how we are getting the exception.
So by using genetics we can a whites situation in Greece all type by using genetics with Dan the compiler
that this is the data type I'm going to use and if I do any mistake then raise a compile time matter.
So this is one of the major advantage of genetic and the other advantage is by using genetic V can reuse
the code.
So the other advantage is called usability.
Let's take an example let's create a class let's plus create a package in this a genetics project.
So we'll create a package.
Calm down basics.
Strong
dart genetics
and head of create a class simply data so we have this class data and inside this data class are we're
going to how we will have a private feel.
Private shrink type data.
Now let's create get those and set those for data and let's have constructor
Soviet constructor and inside this construct let's fight and let's get the definition string data
the data and finally the how do string implementation for this class.
So heavily.
Don
our string data
is
data so data classes ready now let's create one more class to demonstrate the need for genetics.
So let's create one class demo that mean
inside this class.
Let's create object of data glass
new data and we have to pass the data inside it.
And the type for that is strength.
So we have to pass the data in string.
So this is D 1.
Now let's bring
by invoking the two string and let's run this
So data is debunked so everything is fine until now.
But now suppose I want this data to be in Deja What do I do now.
Should I replace the string as integer.
Or then I will not be able to use the string or I would not have the data string.
So what should I do.
Should I play string as object.
So let's do this because I want string as well as in digit objects.
So I will replace this with object
object so I replace it everywhere.
Now inside this demo I can create one more object date.
Daddy do.
And this time I can pass in Deja do it
right.
And if I retrieve the data in data
and
Deven and dart get data.
So as you can see that suppose I mistakenly crossed a Deven to end I actually wanted to cost.
But the cost data back to end but I mistakenly I caused Deven to end
dark get data OK.
So as you can see combined it is not able to detect this mistake.
Now let's run this
so see what gift we got we got.
Class cast exception.
So this is not the solution.
We want to go for right.
So by replacing object object is not appropriate solution that we should go for now should I create
one more class with the data field and having integer as its type yes we can do so.
But next I want to have double as the data.
So should I keep adding classes on answer a simple use genetics how to use genetics.
Let's cut this as you can see this data class in this to make this data class as genetic type if you're
going to use angler brackets to specify the type that meter so then how does had the formal type parameter
d x as a place holder for the actual type that the middle provide.
So we call this D a type that I do so this type.
But I mean did provide a V for us to reuse the same order with any input right.
So this D is a type parameter.
Now I want the type of data to be anything so I will replace it with D.
So now it can be anything
so.
And now this data it says is has become a genetic type.
It's a genetic class.
Now this data is genetic class.
Now let's back to this demo class on this mean metal.
Now let's create Batam address type references to this genetic type data but I'm a trice type of bottom
address type is the barometer that provide information to the compiler that what type is it so that
it can check the types.
So that there's a data Deven I will say this is string
and her so now
the compiler know the type of the data.
So we do not need to costed that compiler does this thing by itself.
Now I can create objects open any data type.
I don't need to write separate classes for that so I can have this as integer
so integer is the bottom address type and add the right.
You do not and you are not needed to specify the parameter based type but you have to specify this diamond.
OPERATOR How what do you can also provide it with integer but this is not necessary to do
so if and as you can see we are getting this error cannot cost string to end it knows what a type it
it what what type it is off.
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
bring data to
and see so data is Dan
one more important thing to keep in mind about genetics is that that this team mean anything they're
using these as a genetic to create an instance of an object but we cannot do this if we have a static
method or feel because genetic type parameters are only in school for an instance of the class that
means instance method any instance fields of over class because static members and fields belong to
each and every instance of the class.
So if you had an instance of data just like here we are having one instance of string data as a string
and data as integer so we have to Einsteins but a different data type then what exactly should the static
method be using string or in Deja said Gondry decided.
So this concept is only applicable for class instances hope you might have understood what's going on
under the hood and it was a quick introduction about genetics.
Hope you understood it.
So now let's move to the next lecture.