Hello
Why can't I call a method with signature
doSomething(Collection<Point> points)
with an argument ArrayList<MyPoint> (where MyPoint extends Point and
ArrayList implements Collection)?
How should I work around this?
Thank you for your answers
Philipp
Example code:
== Test.java ==
import java.awt.Point;
import java.util.ArrayList;
import java.util.Collection;
public class Test {
public static void main(String[] args) {
ArrayList<MyPoint> list = new ArrayList<MyPoint>();
list.add(new MyPoint(1,2,5));
doSomething(list); // gives compile error
}
public static void doSomething(Collection<Point> points){
for(Point p: points){
System.out.println(p);
}
}
}
== MyPoint.java ==
import java.awt.Point;
public class MyPoint extends Point {
public int height;
public MyPoint(int i, int j, int height) {
super(i,j);
this.height = height;
}
}
Hendrik Maryns - 06 Jun 2007 11:22 GMT
Philipp schreef:
> Hello
> Why can't I call a method with signature
> doSomething(Collection<Point> points)
>
> with an argument ArrayList<MyPoint> (where MyPoint extends Point and
> ArrayList implements Collection)?
Because an C<A> does not extend a C<B>, even if B extends A. Thoroughly
read a generics tutorial if you don’t understand why.
> How should I work around this?
Either you change the signature of the method to
void doSomething(Collection<? extends Point> points)
or, if you do not have access to its implementation, wrap your ArrayList
into another one:
List<Point> lessSpecificList = new ArrayList<Point>(myList);
doSomething(lessSpecificList)
But note that this can give problems, if stuff happens to the less
specific list. (In the case below, there is not problem.)
HTH, H.
> Example code:
>
[quoted text clipped - 27 lines]
> }
> }
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
Philipp - 06 Jun 2007 11:46 GMT
Hendrik Maryns a écrit :
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
[quoted text clipped - 6 lines]
>
> void doSomething(Collection<? extends Point> points)
Thanks, I'll do this (and also take your advice to "Thoroughly read a
generics tutorial" :-) )
Philipp
Lew - 06 Jun 2007 14:33 GMT
Philipp schreef:
>>> How should I work around [that Collection<A> does not extend Collection<B> even though A extends B?]
Hendrik Maryns a écrit :
>> Either you change the signature of the method ...
> Thanks, I'll do this (and also take your advice to "Thoroughly read a
> generics tutorial" :-) )
Sun's are pretty good, and cover the issue of why (A <: B) !-> (X<A> <: X<B>).
<http://java.sun.com/docs/books/tutorial/extra/generics/index.html>
which has a page on "Generics and Subtyping":
<http://java.sun.com/docs/books/tutorial/extra/generics/subtype.html>
<http://java.sun.com/docs/books/tutorial/java/generics/index.html>
which has pages on subtyping and wildcards also.
The "Really Big Index" of Sun tutorials is a gold mine:
<http://java.sun.com/docs/books/tutorial/reallybigindex.html>

Signature
Lew
Stefan Ram - 06 Jun 2007 14:48 GMT
>Why can't I call a method with signature
>doSomething(Collection<Point> points)
>with an argument ArrayList<MyPoint> (where MyPoint extends Point and
>ArrayList implements Collection)?
A Collection of Points can do more than a Collection of
MyPoints (i.e., accept a Point), therefore, a Collection of
MyPoints is not a subtype of it.
Assume,
java.util.List<java.lang.String> stringList =
new java.util.ArrayList<java.lang.String>();
Now assume, the following assignement would be allowed:
java.util.List<java.lang.Object> objectList = stringList;
Then one could add an object to this »objectList«:
objectList.add( new java.lang.Object(){} );
However, this would break the type of »stringList«, because
the following call to the get-Operation of the stringList now
will not return a string, because we have been allowed to add
an object above:
java.lang.String string = stringList.get( 0 );