Parameterized classes have expectations of the objects that they reference; they expect certain objects to match their paramaterized types. Heap pollution occurs when a variable of a parameterized class type references an object that it expects to be of the parameterized is not of that parameterized type. Even when heap pollution occurs, the variable is guaranteed to refer to a subclass or subinterface of the declared type, but the object is of a different type. For is not guaranteed to always refer to a subtype of its declared type. For more information on heap pollution, see the Java Language Specification, §4.12.2.1, "Heap PollutionVariables of Reference Type," [JLS 2005]). For instance2014]).
Heap pollution can occur if the program performed some operation involving a raw type that would give rise to a compile-time unchecked warning.
For example, consider the following code snippet.
...
Note that this does not imply that heap pollution only occurs if an unchecked warning actually occurred. It is possible to run a program where some of the binaries were compiled by a compiler for an older version of the Java programming language, or by a compiler that allows the unchecked warnings to suppressed [sic]. This practice is unhealthy at best.
Heap pollution can also occur if the program aliases an array variable of non-reifiable element type through an array variable of a supertype which is either raw or non-generic.
Extending legacy classes and making the overriding methods generic fails because this is disallowed by the Java Language Specification.
...
The use of raw types is allowed only as a concession to compatibility of legacy code. The use of raw types in code written after the introduction of genericity into the Java programming language is strongly discouraged. It is possible that future versions of the Java programming language will disallow the use of raw types.
Note that mixing Mixing generic and raw types is perfectly acceptable if heap pollution can be guaranteed not to allowed provided that heap pollution does not occur.
Noncompliant Code Example
...