Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Raymond Holguin
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Raymond Holguin
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21387
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Henry Wong
author
Marshal
Pie
Posts: 21387
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1051
Eclipse IDE Firefox Browser
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic