fork download
  1. class Generics<T> //T is a user defined data type
  2. {
  3. T data; //syntax: datatype variable_name
  4. Generics (T a){
  5. this.data=a; //this keyword use for data is Generics class attribute
  6. }
  7. /*Here T uses, this method return user T type data.
  8.   Here user can pass any types of any perameter like integer,string,float.
  9.   */
  10. public T getData(){ //method syntax: access_modifier return_type name()
  11. return this.data;
  12. }
  13. }
  14.  
  15. public class Main
  16. {
  17. public static void main(String[] args) {
  18. // syntax class_name<data_type> object_name= new class_name(perameter)
  19. Generics <Integer> obj1= new Generics(15);
  20. System.out.println(obj1.getData());
  21.  
  22. Generics <String> obj2= new Generics("Shakib");
  23. System.out.println(obj2.getData());
  24. }
  25. }
Success #stdin #stdout 0.09s 47004KB
stdin
Standard input is empty
stdout
15
Shakib