using instanceof and casts with generics