M
mrstephengross
I have two classes: Base and Derived (where Derived extends Base). I
also have a Set parameterized on Base (Set<Base>). Can I add a Derived
instance to my set?
The following code demonstrates this, and causes a ClassCastException:
class Base {};
clase Derived {};
Set<Base> set = new TreeSet<Base>();
set.add(new Derived()); // <-- Causes exception
Is there a way to make this work?
Thanks,
--Steve
also have a Set parameterized on Base (Set<Base>). Can I add a Derived
instance to my set?
The following code demonstrates this, and causes a ClassCastException:
class Base {};
clase Derived {};
Set<Base> set = new TreeSet<Base>();
set.add(new Derived()); // <-- Causes exception
Is there a way to make this work?
Thanks,
--Steve