I have written the following code , to check what is the incoming object and add it in the collection but, i get an error given below
class Employee{
}
class BankAcount{
}
class GenericClass<UseT1,UseT2>{
UseT1 t1; UseT2 t2; ArrayList al;
GenericClass(UseT1 t1,UseT2 t2){
this.t1 = t1;
this.t2 = t2;
}
public UseT1 getT1() {
return t1;
}
public void setT1(UseT1 t1) {
this.t1 = t1;
}
public UseT2 getT2() {
return t2;
}
public void setT2(UseT2 t2) {
this.t2 = t2;
}
public void addObject(Object o){
if(o instanceof UseT1)
al.add(o);
}
}
public class GenericTwoTypes {
public static void main(
String[] args) {
GenericClass gc = new GenericClass<Employee, String>(new Employee(),new String());
gc.addObject(new Employee());
}
}
Exception in
thread "main" java.lang.Error: Unresolved compilation problem:
Cannot perform instanceof check against type parameter UseT1. Use instead its erasure Object since generic type information will be erased at runtime
at Generics.GenericClass.addObject(GenericTwoTypes.java:39)
at Generics.GenericTwoTypes.main(GenericTwoTypes.java:49)
Can anybody please tell me how to resolve it.....
Thanks and regards