aspose file tools*
The moose likes Beginning Java and the fly likes Generics: why doesn't this work for extending wildcards? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Generics: why doesn Watch "Generics: why doesn New topic
Author

Generics: why doesn't this work for extending wildcards?

Raymond Holguin
Ranch Hand

Joined: Aug 11, 2009
Posts: 81


from my understand Im basically saying that "list" can take any type which extends ClassA. so im not sure why im able to declare an ArrayList<ClassB> but im not allowed to add any objects of type ClassB???
can someone please help me understand this?
thank you
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
When you declare a reference of type List<? extends ClassA> you are basically saying that this reference can validly refer to any List<X> where X is a ClassA or any specific subtype of ClassA so the following are both valid:


But ClassA is not a closed type. The compiler cannot infer that the only subclass of ClassA is ClassB. This is just as valid:



Since the compiler can't be sure that add(new ClassB) is valid for all references of type List<? extends ClassA> then it won't allow that line to compile.


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Raymond Holguin
Ranch Hand

Joined: Aug 11, 2009
Posts: 81
hmmm...so what i dont get is what the point of being able to do this declaration then



when the only objects I can see to actually add the list are ClassA objects. it seems making that declaration servers no purpose because you can't actually do anything with it.

I saw another example on another site like this


and teh same thing...i can't actually add any Long or any other type that extends number so what is the point?? I might as well just do this


since all i can add are Number objects.

blah wild cards are so confusing to me...
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18717
    
  40


Well, what if you don't need to add anything to the collection? Say you want to traverse a collection, and use methods of a particular class. By having a wildcard as the parameter, you allow the method to work with both the generic of that class, but its subclasses as well (using the ? extends bound).

Henry


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

Joined: Sep 28, 2004
Posts: 18717
    
  40

Raymond Holguin wrote:hmmm...so what i dont get is what the point of being able to do this declaration then



when the only objects I can see to actually add the list are ClassA objects. it seems making that declaration servers no purpose because you can't actually do anything with it.


Actually, you can't add ClassA objects either... try it.

Henry
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
As Henry hinted the real place where wildcards shine is in method parameters. Because of the way generics are implemented in Java, all generic types are invariant. That means the even if you have this class hierarchy:

You still can't do this:

A List<ClassB> is not a subtype of List<ClassA>. But what if you have a method that can deal with a List<ClassA>?


Now you want to process a List<ClassB>. No problem right?

Like we established earlier a List<ClassB> is not a subtype of List<ClassA> so we cant pass it to a method that accepts only List<ClassA>. Without wildcards that means we would have to repeat ourselves for every possible subtype of ClassA.

Now it's a well known fact that programmers are lazy! We don't like to repeat ourselves. Wildcards to the rescue:

Now that method can be used with a List<ClassA>, List<ClassB>, and any other subclass of ClassA that wasn't even conceived when the code was written. Petty cool.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

check this out..........

http://www.coderanch.com/t/506187/java/java/Generic-Questions


SCJP6.0,My blog Ranchers from Delhi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generics: why doesn't this work for extending wildcards?