My answer is a little redundant, but:
Everything that matters <T> means that "my caller knows what type this is, but I do not." So,
AdapterInterface<T> result = null;
Means: "I really do not know what type of result, regardless of what my interlocutor believes that is." The compiler complains about this:
result = new CAdapter();
Since this code cannot assume that T is CClass.
In fact, there is no way to do this without casting (declaring a wild carded method means you need to pass the result when you name it). Throwing is your way of telling the compiler, “I know you have no way of knowing what it is, it's normal: I know. Believe me. Yes, you will get a warning. And that's fine.
Generics do not eliminate all castings, but allow you to do this only once . Instead of making throws everywhere, you need them only in one place, where you, the encoder, know for sure that you play games with types. The rest of the code, using only the adapter that you just created, can safely work with the generic type.
source share