More on Generics in Java

How to define a subclass of a generic class?

Wrong: public class A extends Apple<T>.

Correct: public class A extends Apple<String> or public class A extends Apple (will have a warning).

What is the class of ArrayList<String>?

Try this:

List<String> l1 = new ArrayList<String>();
List<Integer> l2 = new ArrayList<Integer>();

System.out.println( l1.getClass() == l2.getClass() );

Which means Java does not create a new type for ArrayList<String>. Thus, we cannot check generic type using instanceof.


Share this article