wildcard generics

O

Oliver Wong

What is the difference between List<Whatever> and List<? extends
Whatever>?

You can add an instance of Whatever to a List<Whatever>, but you can't
to a List<? extends Whatever>.

- Oliver
 
G

grasp06110

Sounds like it would be useful if Whatever is an abstract class or
interface. If you used ArrayList<Whatever> in this case and added an
instanace of Whatever to the list and then tried to execute an abstract
method you would get a runtime error. By using ArrayList<? extends
Whatever> if you tried to do this you would get a compile time error.
Cool.
 
O

Oliver Wong

Sounds like it would be useful if Whatever is an abstract class or
interface. If you used ArrayList<Whatever> in this case and added an
instanace of Whatever to the list and then tried to execute an abstract
method you would get a runtime error. By using ArrayList<? extends
Whatever> if you tried to do this you would get a compile time error.
Cool.

Note that you can't actually create an instance of an abstract class or
interface; you can only create an instance of one of its subclasses. So
whenever you invoke an abstract method, it would always resolve to some
real, concrete method.

- Oliver
 
B

Bart

What is the difference between List<Whatever> and List<? extends
Whatever>?

AFAIK, ListIterator<Whatever> is not compatible with
ListIterator<SomethingThatExtendsWhatever>.

Regards,
Bart.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,269
Messages
2,571,097
Members
48,773
Latest member
Kaybee

Latest Threads

Top