Generic Restrictions
Everything erasure and the language design forbid — primitives, new T, generic arrays, parameterized instanceof, generic exceptions — plus the type-token workarounds. Interviewers ask why in each case, and the answer is almost always erasure.
part ofJavaoverview, primer and where to startread it →on this pageshowhide
explore
- No Primitive Type Arguments5 questions
- Cannot Instantiate or Array Type Parameters5 questions
- No instanceof with Parameterized Types5 questions
- No Static Members of Type Parameter5 questions
- Generics and Exceptions4 questions
- Overload Erasure Clash4 questions
- Type Tokens and Super Type Tokens4 questions
questions
page 2 of 2How can a generic superclass cause a 'same erasure' clash in a subclass, and what role do bridge methods play?
basics
~20 sWhen you extend a generic class and the type argument fixes a method's parameter, your subclass method can erase to the same signature as another method, causing a clash. The compiler also adds hidden bridge methods so the erased and generic views stay consistent.
Given Java's erasure model, what type information actually survives at runtime, and how do type tokens and super type tokens exploit exactly that to recover what's lost?
basics
~20 sAt runtime Java keeps the raw class of each object but throws away its generic type arguments. Two things survive: an object's own class (so Class tokens work) and the generic Signature metadata of declared classes/fields/methods (so super type tokens can read a baked-in parameterized supertype). Both idioms recover only what's still there.
showing 31–32 of 32