Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Newbie question: Can a parameterized container hold objects derivedfrom the parameter?

Reply
Thread Tools

Newbie question: Can a parameterized container hold objects derivedfrom the parameter?

 
 
mrstephengross
Guest
Posts: n/a
 
      05-23-2008
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
 
Reply With Quote
 
 
 
 
Lars Enderin
Guest
Posts: n/a
 
      05-23-2008
mrstephengross skrev:
> 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?
>

Yes. Replace <Base> with <? extends Base>. See
http://java.sun.com/j2se/1.5.0/docs/.../generics.html.

 
Reply With Quote
 
 
 
 
Owen Jacobson
Guest
Posts: n/a
 
      05-23-2008
On May 23, 11:52*am, mrstephengross <(E-Mail Removed)> wrote:
> 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 {};


I assume you mean class Derived extends Base {} here. Also, Java does
not require semicolons after class declarations.

> * Set<Base> set = new TreeSet<Base>();
> * set.add(new Derived()); // <-- Causes exception


The ClassCastException here has nothing to do with the relationship
between Base and Derived (and as written the error would be a compile-
time error about incorrect types; see previous remark about
'extends' . TreeSet requires that either the elements themselves
implement Comparable or that you provide a Comparator at construction
time. If you don't provide a Comparator, it tries to cast elements to
Comparable when inserting them -- and since neither Base nor Derived
implements Comparable, this causes the CCE you're encountering.

Unless you need a set that's sorted, try another Set implementation
like HashSet that doesn't impose extra constraints on the contents.
If you do need a sorted set, make sure there's something to sort on!
Either make Base implement Comparable (correctly) or provide an
implementation of Comparator for Base when constructing the TreeSet.

-o
 
Reply With Quote
 
mrstephengross
Guest
Posts: n/a
 
      05-23-2008
> The ClassCastException here has nothing to do with the relationship
> between Base and Derived (and as written the error would be a compile-
> time error about incorrect types; see previous remark about
> 'extends' . TreeSet requires that either the elements themselves
> implement Comparable or that you provide a Comparator at construction
> time. If you don't provide a Comparator, it tries to cast elements to
> Comparable when inserting them -- and since neither Base nor Derived
> implements Comparable, this causes the CCE you're encountering.
>
> Unless you need a set that's sorted, try another Set implementation
> like HashSet that doesn't impose extra constraints on the contents.
> If you do need a sorted set, make sure there's something to sort on!
> Either make Base implement Comparable (correctly) or provide an
> implementation of Comparator for Base when constructing the TreeSet.


Ok, I changed it around to implement Comparator. However, I'm still
getting a ClassCastException:

class Foo implements Comparator {
public int compare(Object o1, Object o2) { return 0; } }
class Bar extends Foo {}
SortedSet<Foo> set = new TreeSet<Foo> ();
set.add(new Bar()); // <-- Causes exception!

So Bar extends Foo, which implements Comparator. However, the
exception still occurs. Any ideas?

Thanks,
--Steve
 
Reply With Quote
 
Owen Jacobson
Guest
Posts: n/a
 
      05-23-2008
On May 23, 12:53*pm, mrstephengross <(E-Mail Removed)> wrote:
> > The ClassCastException here has nothing to do with the relationship
> > between Base and Derived (and as written the error would be a compile-
> > time error about incorrect types; see previous remark about
> > 'extends' . *TreeSet requires that either the elements themselves
> > implement Comparable or that you provide a Comparator at construction
> > time. *If you don't provide a Comparator, it tries to cast elements to
> > Comparable when inserting them -- and since neither Base nor Derived
> > implements Comparable, this causes the CCE you're encountering.

>
> > Unless you need a set that's sorted, try another Set implementation
> > like HashSet that doesn't impose extra constraints on the contents.
> > If you do need a sorted set, make sure there's something to sort on!
> > Either make Base implement Comparable (correctly) or provide an
> > implementation of Comparator for Base when constructing the TreeSet.

>
> Ok, I changed it around to implement Comparator. However, I'm still
> getting a ClassCastException:
>
> * class Foo implements Comparator {
> * * public int compare(Object o1, Object o2) { return 0; } }
> * class Bar extends Foo {}
> * SortedSet<Foo> set = new TreeSet<Foo> ();
> * set.add(new Bar()); // <-- Causes exception!
>
> So Bar extends Foo, which implements Comparator. However, the
> exception still occurs. Any ideas?


Yes: you've confused Comparator and Comparable.

Instances of Comparable classes can be compared to other instances of
the same Comparable class.

Instances of Comparator can be used to compare two instances of some
other class.

You probably want Foo to implement Comparable<Foo>.

-o
 
Reply With Quote
 
mrstephengross
Guest
Posts: n/a
 
      05-23-2008
> Yes: you've confused Comparator and Comparable.

Aha! Thank you!

--Steve
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie: can't get custom control to hold contents when posting kbutterly ASP .Net 2 12-06-2006 01:57 PM
Can Template Container hold different type object at a time. Piaoger Gong C++ 2 10-10-2006 05:38 AM
Accessing a container objects state from aggregated objects Derek Basch Perl Misc 4 08-16-2006 09:04 AM
I need a container to hold grid positions and the objects on the grid? Simon L C++ 3 03-31-2006 03:38 PM
do arrays hold pointers or the actual objects don C++ 2 02-08-2004 04:11 AM



Advertisments