I'm having a lot of difficulty casting from one type to antother. My code compiles but I get a class cast exception error at runtime. Can anyone tell me why? Thanks in advance!! FILE 1 import java.util.*; public class SetPlusTest { public static void main(String[] args) { SetPlus s1 = (SetPlus) new HashSet();
SetPlus s2 = (SetPlus) new HashSet(); for(int i=0; i<=8; i+=2) { s2.add(new Integer(i)); } Set u = s1.union(s2); System.out.println(s1 + " union " + s2 + " is " + u); } } FILE 2 import java.util.*; interface SetPlus extends Set { SetPlus union(SetPlus s2); } FILE 3 import java.util.*; public class HashSetPlus extends HashSet implements SetPlus { public SetPlus union(SetPlus s2) { SetPlus z = new SetPlus(s2); z.addAll(this); z.addAll(s2); return z; } } }
Andy Ceponis
Ranch Hand
Joined: Dec 20, 2000
Posts: 782
posted
0
Well your file 1 class name is SetPlusTest, but everywhere else you refer to it as SetPlus. You cant cast anything with a class that doesnt exist. Unless SetPlus is a class that im unaware of which it might be...