Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Generics inheritance

3 views
Skip to first unread message

Philipp

unread,
Jun 6, 2007, 6:06:52 AM6/6/07
to
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

unread,
Jun 6, 2007, 6:22:32 AM6/6/07
to
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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-----

Philipp

unread,
Jun 6, 2007, 6:46:25 AM6/6/07
to
Hendrik Maryns a écrit :

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Philipp schreef:
>
>> How should I work around this?
>
> Either you change the signature of the method to
>
> void doSomething(Collection<? extends Point> points)

Thanks, I'll do this (and also take your advice to "Thoroughly read a
generics tutorial" :-) )

Philipp

Lew

unread,
Jun 6, 2007, 9:33:33 AM6/6/07
to
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 ...

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

Message has been deleted
0 new messages