Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Generic Constructors

Reply
Thread Tools

Generic Constructors

 
 
ipor20@gmail.com
Guest
Posts: n/a
 
      04-01-2006
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@

 
Reply With Quote
 
 
 
 
Ravi
Guest
Posts: n/a
 
      04-01-2006
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?

 
Reply With Quote
 
 
 
 
Piotr Kobzda
Guest
Posts: n/a
 
      04-01-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
Ian Pilcher
Guest
Posts: n/a
 
      04-01-2006
(E-Mail Removed) 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 (E-Mail Removed)
================================================== ======================
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      04-01-2006
On 1 Apr 2006 03:42:26 -0800, (E-Mail Removed) 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
reg constructors/copy constructors inheritance srp113 C++ 3 02-17-2009 04:01 PM
Is the possible to have all the public constructors of the publicbase class as the constructors of a derived class? Peng Yu C++ 5 09-19-2008 10:19 AM
compiler synthesized constructors/copy constructors/assignment operators Jess C++ 5 06-07-2007 11:09 AM
Copy constructors, de/constructors and reference counts Jeremy Smith C++ 2 08-02-2006 11:25 PM
Constructors that call other Constructors Dave Rudolf C++ 12 02-06-2004 03:26 PM



Advertisments