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

Discussion in 'Java' started by mrstephengross, May 23, 2008.

  1. 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
     
    mrstephengross, May 23, 2008
    #1
    1. Advertising

  2. mrstephengross

    Lars Enderin Guest

    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/guide/language/generics.html.
     
    Lars Enderin, May 23, 2008
    #2
    1. Advertising

  3. Re: Newbie question: Can a parameterized container hold objectsderived from the parameter?

    On May 23, 11:52 am, mrstephengross <> 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
     
    Owen Jacobson, May 23, 2008
    #3
  4. Re: Newbie question: Can a parameterized container hold objectsderived from the parameter?

    > 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
     
    mrstephengross, May 23, 2008
    #4
  5. Re: Newbie question: Can a parameterized container hold objectsderived from the parameter?

    On May 23, 12:53 pm, mrstephengross <> 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
     
    Owen Jacobson, May 23, 2008
    #5
  6. Re: Newbie question: Can a parameterized container hold objectsderived from the parameter?

    > Yes: you've confused Comparator and Comparable.

    Aha! Thank you!

    --Steve
     
    mrstephengross, May 23, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Colin Toal
    Replies:
    3
    Views:
    861
    Colin Toal
    Jan 23, 2004
  2. Simon L
    Replies:
    3
    Views:
    413
    Jeff Flinn
    Mar 31, 2006
  3. Piaoger Gong
    Replies:
    2
    Views:
    371
    Sumit Rajan
    Oct 10, 2006
  4. =?iso-8859-1?q?Erik_Wikstr=F6m?=

    Partial specialisation of method taking parameterized parameter

    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Dec 20, 2006, in forum: C++
    Replies:
    3
    Views:
    296
    Victor Bazarov
    Dec 20, 2006
  5. Sean Dockery
    Replies:
    8
    Views:
    152
    Sean Dockery
    Jan 3, 2006
Loading...

Share This Page