Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generic typing confusion in polymorphism

 
Mahesh Murugaiyan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Question(s) on generic typing with polymorphism. The ceritification book says

public void foo(List<?> list) { }
public void foo(List<Object> list) { }


If there IS a difference (and we're not yet saying there is), what is it?

There IS a huge difference. List<?>, which is the wildcard <?> without the keywords extends or super, simply means "any type." So that means any type of List can be assigned to the argument. That could be a List of <Dog>, <Integer>, <JButton>, <Socket>, whatever. And using the wildcard alone, without the keyword super (followed by a type), means that you cannot ADD anything to the list referred to as List<?>.


Q1: If i cant add anything to List<?> what is the point of allowing this?

Consider this example:



Q2: Why im not able to add a Animal object to List<? extends Animal> animals2? doesnt it mean "allow all object that are of type Animal or extends Animal"? it doesnt allow me to add both Animal and Dog.

I have used Generics in Collections since 1.5 for the past 5 yrs or so. Still, this behaviour is puzzling me. Ref materials are not helping too much either. hence the long post!

Thanks in advance.
 
Henry Wong
author
Marshal
Pie
Posts: 21212
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mahesh Murugaiyan wrote:
Q1: If i cant add anything to List<?> what is the point of allowing this?

Consider this example:




With your example, there is no point. Why would you instantiate a generic list, and tell the compiler to forget the type? Using wildcards allow operations on multiple types, for example if List<?> was a method parameter, then it can take a list of any generic type. Of course, the method can't add anything, but what if you don't need to add anything, then you can write a method that takes many types of lists.

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 21212
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mahesh Murugaiyan wrote:
Q2: Why im not able to add a Animal object to List<? extends Animal> animals2? doesnt it mean "allow all object that are of type Animal or extends Animal"? it doesnt allow me to add both Animal and Dog.


Actually no. It means it is a List<Animal> or List<Dog> or List<Cat> or another list of some animal that extends Animal -- and the compiler doesn't know which type it is. You can't add Animal, because the list could be a List<Dog> which doesn't allow adding an Animal instance. You can't add Dog, because the list could be a list<Cat> which doesn't allow adding an Dog instance.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic