Generic Constructors

Discussion in 'Java' started by ipor20@gmail.com, Apr 1, 2006.

  1. Guest

    I'm trying to find a way of allowing me to create typed Vectors - in
    the past I've been able to use generics to achieve this (for example
    new Vector<Integer>) but at the moment I'd like to declare a Vector
    field in a base class (with no particular type or maybe another base
    class) and then assign a type to it in the constructor of classes
    extending the base class - I was wondering if this is possible - I'm
    thinking along the following lines...

    class BaseContainer{
    Vector children = new Vector() ;

    }


    class Type1Container extends BaseContainer{
    public Type1Container(){
    children = new Vector<Type1>() ;
    }
    }
    class Type2Container extends BaseContainer{
    public Type2Container(){
    children = new Vector<Type2>() ;
    }
    }
    Any ideas welcome

    P@
     
    , Apr 1, 2006
    #1
    1. Advertising

  2. Ravi Guest

    Declare the Vector in the base container as class variable.
    In the extended class constructor initialized that variable as you wish


    protected Vector children ;
    class BaseContainer{
    }


    class Type1Container extends BaseContainer{
    public Type1Container(){
    children = new Vector<Type1>() ;
    }

    }


    class Type2Container extends BaseContainer{
    public Type2Container(){
    children = new Vector<Type2>() ;
    }

    }

    Is it clear?
     
    Ravi, Apr 1, 2006
    #2
    1. Advertising

  3. Piotr Kobzda Guest

    wrote:

    > I'm trying to find a way of allowing me to create typed Vectors - in
    > the past I've been able to use generics to achieve this (for example
    > new Vector<Integer>) but at the moment I'd like to declare a Vector
    > field in a base class (with no particular type or maybe another base
    > class) and then assign a type to it in the constructor of classes
    > extending the base class (...)


    Make your BaseContainer a generic class like this:

    class BaseContainer<T> {
    Vector<T> children = new Vector<T>();
    }

    or like this:

    class BaseContainer<T extends TypeBase> {
    Vector<T> children = new Vector<T>();
    }


    And than simply use this way:

    class Type1Container extends BaseContainer<Type1> {

    ...

    You don't have to reinitialize your container instance variable with a
    new Vector parameterized with different type until derived classes needs
    a Vector as a collection. BTW -- Consider using List instead of Vector
    as your instance variable type.


    piotr
     
    Piotr Kobzda, Apr 1, 2006
    #3
  4. Ian Pilcher Guest

    wrote:
    >
    > class BaseContainer{
    > Vector children = new Vector() ;
    >
    > }


    class BaseContainer<T>
    {
    Vector<T> children = new Vector<T>();
    }

    > class Type1Container extends BaseContainer{
    > public Type1Container(){
    > children = new Vector<Type1>() ;
    > }
    > }


    class Type1Container extends BaseContainer<Type1> {}

    > class Type2Container extends BaseContainer{
    > public Type2Container(){
    > children = new Vector<Type2>() ;
    > }
    > }


    class Type2Container extends BaseContainer<Type2> {}

    HTH

    --
    ========================================================================
    Ian Pilcher
    ========================================================================
     
    Ian Pilcher, Apr 1, 2006
    #4
  5. Roedy Green Guest

    On 1 Apr 2006 03:42:26 -0800, wrote, quoted or
    indirectly quoted someone who said :

    >class BaseContainer{
    > Vector children = new Vector() ;
    >
    >}
    >
    >
    >class Type1Container extends BaseContainer{
    > public Type1Container(){
    > children = new Vector<Type1>() ;
    > }


    Look at how Vector itself works. Your base class is going to have to
    have a parameterised type as well as classes that extend it, up to the
    point you finally decide on the contained type.

    Are you sure you meant Vector, not ArrayList. Vector is almost
    deprecated.
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Apr 1, 2006
    #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. Dave Rudolf
    Replies:
    12
    Views:
    8,331
    Martijn Lievaart
    Feb 6, 2004
  2. Jeremy Smith
    Replies:
    2
    Views:
    603
    Jeremy Smith
    Aug 3, 2006
  3. Jess
    Replies:
    5
    Views:
    615
    Ron Natalie
    Jun 7, 2007
  4. Peng Yu
    Replies:
    5
    Views:
    403
    Juha Nieminen
    Sep 19, 2008
  5. srp113
    Replies:
    3
    Views:
    482
Loading...

Share This Page