} Which of teh following can be inserted here to compile and run without error? choose all that apply A. return s B. return(Object) s; C. return s.toString(); D. return new StringBuilder(s); E. return (S) new StringBuilder(s); F. return null;
The answer is A. and F why not D or E??
If you worry you cannot work... If you work you need not worry
You can't return a stringbuilder or stringbuffer because you're not guaranteed that one of those objects are going to be the one "created" in the generic function. <S> could be something other than a buffer or builder -- or something that cannot be stuck into the constructor of StringBuilder, and then the return would be incorrect and not match what the method header says.
The keyword extends is somewhat misleading because it also applies to interfaces. In your example the interface is the generic type. String and Stringbuffer are a classes and not an interface so they could not be returned.
If they implement the interfaces you can use them, but only the methods which are defined by the interface. So be aware extends in generics could also apply for interfaces.