Generic type information exists at compile time and is erased in the bytecode, where a List of String is just a List with casts inserted by the compiler. So you cannot check instanceof against a parameterized type, create an array of a type parameter, or overload two methods that differ only by generic argument. The compiler adds bridge methods to keep polymorphism working, and unchecked casts can cause heap pollution.
Why interviewers ask this
It explains a whole family of confusing compiler errors, so the interviewer wants to see you understand the mechanism rather than working around it by feel. Mentioning bounded wildcards and the producer extends consumer super rule shows you can design a generic API, not just consume one. It also comes up whenever someone needs the actual type at runtime, for example in a deserializer.
How to structure your answer
- Define erasure and what the bytecode actually contains.
- List the concrete restrictions it causes.
- Explain the wildcard rule for flexible signatures.
- Say how you recover type information at runtime when you must.
Example answer
Generics are a compile time feature. After compilation a List of String is just a List, with the compiler having inserted the casts, which is what kept generics backward compatible. The visible consequences are that I cannot ask if something is an instance of a parameterized type, I cannot instantiate an array of a type parameter without an unchecked cast, and I cannot overload a method on two different generic arguments because both erase to the same signature. It is also why an unchecked cast warning matters: a wrongly typed list can hold the wrong element and fail later at some unrelated cast, which is heap pollution and is genuinely annoying to trace. When I design an API I use bounded wildcards to make it usable, following the producer extends consumer super rule, so a method reading from a collection takes a wildcard extending the type. When I really need the type at runtime, for example telling a JSON library what to deserialize into, I pass a class object or use a type reference that captures the parameter through an anonymous subclass, since the type argument on a class is retained in the signature.
Walking into this interview soon? GhostPilot listens to your live call, spots the question the moment it is asked, and puts a structured answer on your screen in real time. Try it on your next mock, or grab a $29 Session Pass, no subscription, for the real thing.
See how it worksFollow-up questions to expect
- What does the producer extends consumer super rule mean in practice?
- Why does the compiler generate bridge methods?
- How does a type reference capture a generic type despite erasure?
Related java developer questions
Your interviewer will ask their own version of this. Paste your actual job description into the free Question Predictor and get the 20 questions that role is most likely to ask, with what each one is really probing.
Predict my questions