Create Generic Class
* Generic Class to handle parameterized types.
* Example if class name called Employee. And the ID field may be some cases string and some usecase it should be number. Then at run time we can specify the DataType and we can make use of the same class.
Eg.
public class Employee<T> {
private T id;
public void add(T id) {
this.id = id;
}
public T get() {
return id;
}
public static void main(String[] args) {
Employee<Integer> integerEmployee = new Employee<Integer>();
Employee<String> stringEmployee = new Employee<String>();
integerEmployee.add(new Integer(1001));
stringEmployee.add(new String("Hello World"));
System.out.printf("Integer Value :%d\n\n", integerEmployee.get());
System.out.printf("String Value :%s\n", stringEmployee.get());
}
}
<T> -> It can be any alphabet. To maintain the standard naming convertion for Type, we just using T
Comments
Post a Comment