You are here: Home / Topics / Program to explain Simple Generic class in Java

Program to explain Simple Generic class in Java

Filed under: Java on 2024-04-12 18:32:56

// Program to explain Simple Generic class.

class   A<T> 
{
T ob;

A(T o) 
{
 ob = o;
}

T getob() 
{
 return ob;
}

void showType() 
{
 System.out.println("Type of T is " + ob.getClass().getName());
}
}

public class  GenericTest1
{
public static void main(String args[ ]) 
{
 A<Integer> iOb = new A<Integer>(100);
 iOb.showType();

 int v = iOb.getob();
 System.out.println("Value: " + v);

 System.out.println();
 A<String> strOb = new A<String>("Nils");
 strOb.showType();

 String str = strOb.getob();
 System.out.println("Value: " + str);
}
}


Output:

Type of T is java.lang.Integer
Value: 100

Type of T is java.lang.String
Value: Nils


About Author:
J
Java Developer     View Profile
Hi, I am using MCQ Buddy. I love to share content on this website.