aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generic typing confusion in polymorphism Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generic typing confusion in polymorphism" Watch "Generic typing confusion in polymorphism" New topic
Author

Generic typing confusion in polymorphism

Mahesh Murugaiyan
Greenhorn

Joined: Jun 25, 2009
Posts: 21
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
Sheriff

Joined: Sep 28, 2004
Posts: 19060
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19060
    
  40

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generic typing confusion in polymorphism