 Hello friends, I am Sanjay Gupta. In this video, I am going to demonstrate you the concept of generics in Java. Before starting, you can subscribe my YouTube channel by clicking on the link which is available at the bottom right corner of this video. You can download my app Takimiz that is available on Google Play. You can connect with me by following these details which are available in front of you. Now coming onto the topic, first I am explaining what is generics in Java. So first point, through the use of generics, it is possible to create classes, interfaces and methods that will work in a type safe manner with various kinds of data. So if you want to create a class, interface or method that can work with various kind of data, then you can apply generics. Many algorithms are logically the same no matter what type of data they are being applied to. For example, the mechanism that supports a stack is the same whether that stack is storing items of type integer, string, object or thread. So if we talk about stack, stack process is same for any type of data that we want to process. So with generics, you can define an algorithm once independently of any specific type of data and then apply that algorithm to a wide variety of data types without any additional effort. So it means we can apply the same algorithm for any particular data type which is available. And this can be happened with the help of generics. Thus, expressive power generics added to the language fundamentally changed the way that Java code is written. So now I am going to demonstrate you an example so that you can understand this concept which I have explained right now. So here you can see I have implemented a class. It is containing a comment here. T is a type parameter. So this T is a type parameter that will be replaced by a real type when an object of type GEN is created. So GEN is name of class when object of this gen class will be created, then a particular data type will be available in place of T. So T will be replaced automatically while creation of object of this class. You can see I have created an object OB of type T here. Then this GEN is a constructor. This constructor is receiving a reference of type T and that reference is assigned into OB. After the constructor, this method getOB is defined which is returning OB. So written type of getOB is T because OB's data type is T. So you can see that constructor and method both are using data type as T because any specific data type is not available right now. Then third method is showType which is accessing ob.getClass.getName. So ob is a reference which is accessing getClass method and getClass method is accessing getName. So the type which is assigned for ob that type's class will be accessed here and that name will be accessed with the help of getName method. Now I am going on this second class which is genDemo which is accessing or sorry which is defining main method. Inside this main method, the statement is important. So gen is the name of class. It is creating an object ob with the help of new. You can see that in Angular records, integer is written here. So this ob object will be working for integers. So this T will be replaced by integer right now for this ob object. So this is the benefit of generics. This object will work for integers and this 88 is passed as integer value that will be received by this O and this O will be assigned into ob. So friends, this ob object will be working for integers. Then ob.showType, this will display type of the data type which I have used right now which is integer. Then iob.gatobi, it will return the value and that value will be stored inside V. So here auto unboxing concept is used and this statement is using auto boxing concept. So you can watch videos related to auto boxing and auto unboxing in my playlist which you are right now seeing. And this statement is printing value of V which is received here by calling the method gatobi. So gatobi is returning an integer value which is received in V and V is displaying this value using system.out.println. So similar to these statements, another four statements are defined. Here strob is object which is using string as data type and message is generic test. So here this T will be replaced with string data type. So friends, you can see that same class, gen class is creating two objects and both the objects are working for two different data types. One for integer and another for string. And remaining code is same. This show type will display type of data type. Then this gatobi will return the data into str and str will display it back on console. So now I am going to execute this code so that you can see the output. You can see type of T is java.lang.integer that is displayed with the help of this method. This statement is printing this java.lang.integer. Then value 88, this is displayed through this statement. It is displaying value of V which is received by gatobi method. Then type of T is java.lang.string. It is for second object strobi. And then value generic test is displayed using this system.out.println statement. So friends, if you want to apply multiple data types or more than one data types on a same class then you have to implement that class as generic class as I have implemented for you. So this demonstration demonstrated how you can implement generics in Java. So I hope you have understood the concept well. If you want to watch more programming related videos, you can follow my YouTube channel. Do subscribe my YouTube channel. Keep connected with me. Thank you for watching this video.