skip to content

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 pageshow

questions

page 2 of 2

How can a generic superclass cause a 'same erasure' clash in a subclass, and what role do bridge methods play?

level: principalimportance: nice to knowfreq 18%

basics

~20 s

When 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.

open as a page

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?

level: principalimportance: nice to knowfreq 25%

basics

~20 s

At 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.

open as a page

showing 31–32 of 32