Hello,
I have a little problem with the use of generic methods to refactor code. I actually trying to instantiate an object in a generic method as follows:
with class B inherits class A. Obviously I'll give you a thousand, the compiler can not dip that anObject is an instance of A or its sub-classes and I released two best big error "incompatible types" (found A required X and B found required X ). If I cast upon instantiation, then I am entitled to the usual warning "unchecked cast". How to get out of there without error or warning ? Why in the methods generic substitution is not made (as in a generic class, any object type X has access to methods defined in A)?Code:private <X extends A> X instanciationSelective(boolean Class) { X = anObject null; if (Class) anObject = new Has(); else anObject = new B(); return anObject }
Bookmarks