• Post Reply Bookmark Topic Watch Topic
  • New Topic

more trouble with Generics - inserting in to a base class  RSS feed

 
Stuart Rogers
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello again,

following up on my previous post, I've created an interface to handle calls to a function getAsArray which exists in each of several object classes
ie. Asset_HP, Asset_GPS, etc.

Using generics to write out the various lists works great (thanks guys!)

Now I'd like to use generics to have one function to populate any list rather than one function for each list due to having to hardcode the call to the
object class' constructor.




Any ideas/suggestions/constructive criticisms are all appreciated.


TIA,

Still-learning Stuart
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you really using different methods to handle assets from different manufacturers? That looks very non-object-oriented to me.
 
Stuart Rogers
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your reply.

Having learned that the type information of an object is thrown away when you place it into a Collection object

LinkedList mylist = new LinkedList();
Cat blackCat = new Cat("black");
mylist.add( blackCat);

favoritePet = mylist.get(0); // errors because favoritePet doesn't know it came from a Cat

But what about when I don't use a collection object "in the raw"?

LinkedList<Cat> mylist = new LinkedList<Cat>();
Cat blackCat = new Cat("black");
mylist.add( blackCat);

favoritePet = mylist.get(0); // I'd hoped to make this work somehow

Or is it possible to extract the type of the stored objects ( Cat ) from a Collection with a specified type?

Still-learning Stuart








 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Collections don't store the actual parameter; so the <Cat> bit is lost. This is because Java implements generics by erasure.

What I meant about object-oriented is that you should have a field in your resource which stores the manufacturer's name. You can put those names into an enum.
 
Stuart Rogers
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Back again armed with more code.

Essentially, I have several kinds of similar assets and I'd like to place each kind of asset in its own list.
Creating a class for each specific type of asset and having them all share an interface to expose a common method seems logical.

Been busy reading and it sounds like Generics, with a little help from reflection, will do what I need.
So I created a generic AssetLinkedList class that contains a .populate() and writeOut() method.
The idea is to instantiate several AssetLinkedList objects and populate them from some csv files.
The key is for each AssetLinkedList object to intrinsically know which kinds of assets it will be filled with.

The following code compiles without errors but fails at runtime on line 164 (I've flagged in in the code with a comment ) with the message:

Exception in thread "main" java.lang.IllegalArgumentException: wrong number of arguments
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at gps.lisa.AssetLinkedList.populate(Lisa.java:164)
at gps.lisa.Lisa.main(Lisa.java:323)

So it seems to be pretty close. Any help/suggestions/hints/constructive criticism is always welcome. Like, is there a
better way for a instance AssetLinkedList to know what type of object it should contain without having to pass in an example
of the object?

Still-learning Stuart

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!