M
Marc-Andre Michel
Hi,
Could somebody tell me why the following code doesn't work ?
I presume the reason is that the type to cast to is not known at
compile time. If this is the reason,
what is your suggestion so A() can call the right method for the right
object type ?
public class A {
public A(Object obj) {
foo(obj); //ERROR
}
public void foo(String s) {
//...print a string
}
public void foo(Collection c) {
//...print a collection of strings
}
public static void main(String[] args) {
String s = "my string";
A a1 = new A(s);
ArrayList l = new ArrayList();
l.add("s1");
l.add("s2");
l.add("s3");
A a2 = new A(l);
}
}
Thanks,
Marc
Could somebody tell me why the following code doesn't work ?
I presume the reason is that the type to cast to is not known at
compile time. If this is the reason,
what is your suggestion so A() can call the right method for the right
object type ?
public class A {
public A(Object obj) {
foo(obj); //ERROR
}
public void foo(String s) {
//...print a string
}
public void foo(Collection c) {
//...print a collection of strings
}
public static void main(String[] args) {
String s = "my string";
A a1 = new A(s);
ArrayList l = new ArrayList();
l.add("s1");
l.add("s2");
l.add("s3");
A a2 = new A(l);
}
}
Thanks,
Marc