10.1 Generic introduction
Hello everyone welcome to this new course in this course we will talk about generics The generics are classes that have types at the time of compilation Sometimes we want to create a class but we do not want to specify the type of this or that attribute It is according to the case when there class serves as a container or another type of class In java it is possible to create methods or classes so some types are parameters that will be resolved at the time of invocation and indentation This is when we talk about a method Generic class You have already seen this concept with the Argalis There with the Argalis Even with the LinkedList We'll take an example We'll create a family class We'll name our class Family so refactor We'll put Family And in our class we'll put several attributes So the name The first name And to finish we'll put the Age And then we'll generate the constructors And the getters Setters too And we'll create an instance in our class Family in our class Main Family So the name And the first name And to finish the Age So we will put 12 and we will remake another Instance of family And the Age We will put two rather a String so the child is two years old We put a String You see that here we have an error Given that here our constructor expects a value in integer It is in this kind of situation That the genericity applies situation that the genericity applies If we want to keep two in the form of character string It will be necessary that we come in our Class Family And to replace the Int in String Therefore in character string therefore And to make its You saw that the first the first forgives Instance Also has an error given that we wait for a character string Therefore to palliate has this problem Java to bring back the Gene recite in the version 5 To do it it is necessary to return in family And in family in our class We are going to create a generic object So it is to say that here instead of being limited to a String or an Int or has other type of value We are going to Put in parameter of our a generic type So to do it it is necessary to put in crocher So there we note the presence of a parameter of type Name here T It is used to specify that in the definition of class that will follow T represents any type it can be a String a String in or even other Here the name of the parameter you could put everything you wanted but to increase the readability of the code It is recommended to use a single letter in capital letters. Now I'm going to show you the different letters that you can use to increase the readability of the code. So now the type that I receive as a parameter I'm going to put it in all the places that I want to be dynamic, for example, for the Age I can remove the String and put T. And then I'll have to do the same for the Age parameter. Of course we will display both And we will compile our program As you can see Java has directly recognized that an integer and a string In the old version you had to specify The type you put here we put an integer So we will put integer And there we will put A string A string a String You had to do this for the old versions of Java of Java And that will work correctly It is necessary to know that the only constraint to respect at this level and this type must obligatorily be a class So you could not put An integer like this That will not work It is necessary to put the class directly I hope that you understood this course In the next course which are going to follow we are going to deepen on the generic so to soon