Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2006

Tip: Looking for answers? Try searching our database.

Generic Constructors

Thread view: 
ipor20@gmail.com - 01 Apr 2006 12:42 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 - 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.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.