class Generics<T> //T is a user defined data type
{
T data; //syntax: datatype variable_name
Generics (T a){
this.data=a; //this keyword use for data is Generics class attribute
}
/*Here T uses, this method return user T type data.
Here user can pass any types of any perameter like integer,string,float.
*/
public T getData(){ //method syntax: access_modifier return_type name()
return this.data;
}
}
public class Main
{
public static void main
(String[] args
) { // syntax class_name<data_type> object_name= new class_name(perameter)
Generics <Integer> obj1= new Generics(15);
System.
out.
println(obj1.
getData());
Generics <String> obj2= new Generics("Shakib");
System.
out.
println(obj2.
getData()); }
}
Y2xhc3MgR2VuZXJpY3M8VD4gIC8vVCBpcyBhIHVzZXIgZGVmaW5lZCBkYXRhIHR5cGUKewogIFQgZGF0YTsgICAvL3N5bnRheDogZGF0YXR5cGUgdmFyaWFibGVfbmFtZSAKICBHZW5lcmljcyAoVCBhKXsKICAgICB0aGlzLmRhdGE9YTsgIC8vdGhpcyBrZXl3b3JkIHVzZSBmb3IgZGF0YSBpcyBHZW5lcmljcyBjbGFzcyBhdHRyaWJ1dGUgCiAgfQogIC8qSGVyZSBUIHVzZXMsIHRoaXMgbWV0aG9kIHJldHVybiB1c2VyIFQgdHlwZSBkYXRhLiAKICBIZXJlIHVzZXIgY2FuIHBhc3MgYW55IHR5cGVzIG9mIGFueSBwZXJhbWV0ZXIgbGlrZSBpbnRlZ2VyLHN0cmluZyxmbG9hdC4KICAqLwogIHB1YmxpYyBUIGdldERhdGEoKXsgIC8vbWV0aG9kIHN5bnRheDogYWNjZXNzX21vZGlmaWVyIHJldHVybl90eXBlIG5hbWUoKQogICAgICByZXR1cm4gdGhpcy5kYXRhOwogIH0KfQoKcHVibGljIGNsYXNzIE1haW4KewoJcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nW10gYXJncykgewogLy8gc3ludGF4IGNsYXNzX25hbWU8ZGF0YV90eXBlPiBvYmplY3RfbmFtZT0gbmV3IGNsYXNzX25hbWUocGVyYW1ldGVyKQoJCUdlbmVyaWNzIDxJbnRlZ2VyPiBvYmoxPSBuZXcgR2VuZXJpY3MoMTUpOwoJCVN5c3RlbS5vdXQucHJpbnRsbihvYmoxLmdldERhdGEoKSk7CgkJCgkJR2VuZXJpY3MgPFN0cmluZz4gb2JqMj0gbmV3IEdlbmVyaWNzKCJTaGFraWIiKTsKCQlTeXN0ZW0ub3V0LnByaW50bG4ob2JqMi5nZXREYXRhKCkpOwoJfQp9