P
Philipp
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;
}
}
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;
}
}