Question about Implementing Generics

Discussion in 'Java' started by David, May 9, 2005.

  1. David

    David Guest

    To All,

    Is there a way when I am creating my own generic class that I can define
    the (for lack of a better term) highest super class allowed as the
    generic?

    To be more specific, for example, if you use define a Vector and don't
    utilize the generics, an Object will be returned via the elementAt()
    method. However, let's say I have written a MyObject class that extends
    Object and I have a MyVector that extends Vector and I want to define
    within MyVector that it only allows MyObject or other extensions of it
    so that if a MyVector is instantiated with the generics definition, a
    MyObject is returned, not an Object.

    Is that clear?

    Sniplets that may help or hinder......

    class MyObject extends Object {
    public void someMethod() { // do something }
    }

    class MyVector<V> extends Vector {
    /* I want to force V to be an instance of MyObject */
    public V elementAt(int index) { return super.elementAt(index); }
    }

    class Tester {
    public static void main(String[] args) {

    Vector v = new Vector();
    v.add("A");
    v.elementAt(0); // returns an Object.

    Vector<String> v2 = new Vector<String>();
    v.add("A");
    v.elementAt(0); // returns a String.

    MyVector mv = new MyVector();
    mv.add(new MyObject());
    /* The next line will fail because the default is to return an object
    but I want it to be a MyObject. */
    mv.elementAt(0).someMethod();
    David, May 9, 2005
    #1
    1. Advertising

  2. David

    Daniel Dyer Guest

    On Mon, 09 May 2005 23:51:48 +0100, David <> wrote:

    > class MyVector<V> extends Vector {
    > /* I want to force V to be an instance of MyObject */
    > public V elementAt(int index) { return super.elementAt(index); }
    > }


    If I understand you correctly, the following may be sufficient:

    class MyVector extends Vector<MyObject> {

    That's if you don't want to specify the type of your collection when you
    declare a field/variable of type MyVector.

    private MyVector myField;

    Otherwise, this should work:

    class MyVector<T> extends Vector<T> {

    Then you declare your field like this:

    private MyVector<MyObject> myField;

    In both cases myField.elementAt() should return a MyObject instance with
    no need for casting. No need in either case to over-ride the elementAt
    method.

    Dan.

    --
    Daniel Dyer
    http://www.footballpredictions.net
    Daniel Dyer, May 10, 2005
    #2
    1. Advertising

  3. David

    Daniel Dyer Guest

    On Tue, 10 May 2005 00:11:49 +0100, Daniel Dyer
    <> wrote:

    > On Mon, 09 May 2005 23:51:48 +0100, David <>
    > wrote:
    >
    >> class MyVector<V> extends Vector {
    >> /* I want to force V to be an instance of MyObject */
    >> public V elementAt(int index) { return super.elementAt(index); }
    >> }

    >
    > If I understand you correctly, the following may be sufficient:
    >
    > class MyVector extends Vector<MyObject> {
    >
    > That's if you don't want to specify the type of your collection when you
    > declare a field/variable of type MyVector.
    >
    > private MyVector myField;
    >
    > Otherwise, this should work:
    >
    > class MyVector<T> extends Vector<T> {
    >
    > Then you declare your field like this:
    >
    > private MyVector<MyObject> myField;
    >
    > In both cases myField.elementAt() should return a MyObject instance with
    > no need for casting. No need in either case to over-ride the elementAt
    > method.


    Having read your post again, I realise that my second solution doesn't
    solve your problem. The first one may still be sufficient, if not this
    should do the trick:

    class MyVector<T extends MyObject> extends Vector<MyObject> {

    Dan.

    --
    Daniel Dyer
    http://www.footballpredictions.net
    Daniel Dyer, May 10, 2005
    #3
  4. David

    Chris Smith Guest

    Daniel Dyer <> wrote:
    > Having read your post again, I realise that my second solution doesn't
    > solve your problem. The first one may still be sufficient, if not this
    > should do the trick:
    >
    > class MyVector<T extends MyObject> extends Vector<MyObject> {


    I think you meant:

    class MyVector<T extends MyObject> extends Vector<T>

    --
    www.designacourse.com
    The Easiest Way To Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
    Chris Smith, May 10, 2005
    #4
  5. David

    Daniel Dyer Guest

    On Tue, 10 May 2005 00:33:26 +0100, Chris Smith <> wrote:

    > Daniel Dyer <> wrote:
    >> Having read your post again, I realise that my second solution doesn't
    >> solve your problem. The first one may still be sufficient, if not this
    >> should do the trick:
    >>
    >> class MyVector<T extends MyObject> extends Vector<MyObject> {

    >
    > I think you meant:
    >
    > class MyVector<T extends MyObject> extends Vector<T>


    Only if you want type-safety :)

    My version is effectively the same as the first suggestion I posted, but
    with an unenforced suggestion as to the type of the contents. It's past
    my bedtime.

    Thanks,

    Dan.



    --
    Daniel Dyer
    http://www.footballpredictions.net
    Daniel Dyer, May 10, 2005
    #5
    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. geoffrey wall

    question on generics, constants in vhdl

    geoffrey wall, Oct 1, 2005, in forum: VHDL
    Replies:
    1
    Views:
    644
    Mike Treseler
    Oct 1, 2005
  2. Roger Levy
    Replies:
    20
    Views:
    892
    Neal Gafter
    Jan 5, 2005
  3. Joona I Palaste

    Generics question

    Joona I Palaste, Oct 14, 2004, in forum: Java
    Replies:
    6
    Views:
    475
    Vincent Cantin
    Oct 15, 2004
  4. Juergen Berchtel
    Replies:
    1
    Views:
    5,990
    John C. Bollinger
    May 20, 2005
  5. Soul
    Replies:
    0
    Views:
    518
Loading...

Share This Page