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;
}
}
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:
>
> == 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
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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFGZoroe+7xMGD3itQRAvRrAJ9TraWDw4PnL0EeWzThqd/4TT54rwCfToOl
vILY2FwgDXHlKHVDZP0VVBQ=
=sEJg
-----END PGP SIGNATURE-----
Thanks, I'll do this (and also take your advice to "Thoroughly read a
generics tutorial" :-) )
Philipp
Hendrik Maryns a écrit :
>> Either you change the signature of the method ...
Philipp wrote:
> 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>
--
Lew