Java Generics: (extends List<E>) vs (extends List)

1.3k Views Asked by At

Assume I want to declare an interface named A which extends class List. What is the difference if I write as A<E> extends List or A<E> extends List<E> ? What is the difference between objects instantiated form these two classes?

The question remains about methods or classes with bounded type parameters. like

public <T extends aClass<T>> void methodName(T t)

and

public <T extends aClass> void methodName(T t) .


CLARIFICATION:

I know what is raw type. Suppose we have this:

Class someClass<E> extends ArrayList

someClass is subclass of ArrayList, means it inherits all accessible fields and methods of ArrayList.

I know if I replace ArrayList with ArrayList<E>, if I invoke someClass<String> object it would pass String to the super class which is ArrayList and that's fine.

But let's stick with the code. Now if i invoke someClass<String> object, it doesn't pass String to the ArrayList. So now, object is raw or not and how its fields and methods work?

1

There are 1 best solutions below

0
On
A<E> extends List

should never be used, since List is a raw type.

You should either use:

interface A<E> extends List<E>

if A should be a List of some generic element type.

or:

interface A extends List<String> // or any concrete type instead of String

if A should be a List of a specific element type.