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@
Ravi - 01 Apr 2006 12:57 GMT
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?
Piotr Kobzda - 01 Apr 2006 16:39 GMT
> 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
Ian Pilcher - 01 Apr 2006 17:39 GMT
> 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

Signature
========================================================================
Ian Pilcher i.pilcher@comcast.net
========================================================================
Roedy Green - 01 Apr 2006 19:18 GMT
>class BaseContainer{
> Vector children = new Vector() ;
[quoted text clipped - 5 lines]
> 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.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.