• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Generic typing confusion in polymorphism

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
The human mind is a dangerous plaything. This tiny ad is pretty safe:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic