Polymorphism
From: Marc-Andre Michel (nosferatu_1_1_at_yahoo.com)
Date: 07/31/04
- Next message: John Fereira: "Re: Conditional import in Java?"
- Previous message: paul.foreman: "Applet connecting to a Server"
- Next in thread: Manish Hatwalne: "Re: Polymorphism"
- Reply: Manish Hatwalne: "Re: Polymorphism"
- Maybe reply: Manish Hatwalne: "Re: Polymorphism"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 31 Jul 2004 06:50:21 -0700
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
- Next message: John Fereira: "Re: Conditional import in Java?"
- Previous message: paul.foreman: "Applet connecting to a Server"
- Next in thread: Manish Hatwalne: "Re: Polymorphism"
- Reply: Manish Hatwalne: "Re: Polymorphism"
- Maybe reply: Manish Hatwalne: "Re: Polymorphism"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|