Generic Programming example -1
//Here T is name of type parameter
// this name is used as a placeholder for the actual type
//that will be passed to Gen when an object is created
class GenericDemo<T>
{
T obj;
// Pass the constructor a reference to an Object of type T
public GenericDemo(T obj) {
this.obj = obj;
}
// return Object
public T getObj() {
return obj;
}
void showType()
{
System.out.println("Type of T is :" + obj.getClass().getName());
}
}
public class GenericTypeTesting {
public static void main(String[] args) {
GenericDemo<Integer> intojb = new GenericDemo<Integer>(210);
System.out.println(intojb.getObj());
intojb.showType();
GenericDemo<String> strobj = new GenericDemo<String>("ISTAR");
System.out.println(strobj.getObj());
strobj.showType();
}
}
Comments
Post a Comment