Generic Constructors

I

ipor20

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@
 
R

Ravi

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?
 
P

Piotr Kobzda

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
 
I

Ian Pilcher

class BaseContainer{
Vector children = new Vector() ;

}

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

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

class Type2Container extends BaseContainer<Type2> {}

HTH
 
R

Roedy Green

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top