aspose file tools*
The moose likes Java in General and the fly likes Need help with Generics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Need help with Generics" Watch "Need help with Generics" New topic
Author

Need help with Generics

Vinicius Alves
Greenhorn

Joined: Dec 21, 2010
Posts: 14
I have:









My question is , How do I create a instance of GenericSearch ?
I have tried Google and other forums and I coud'nt get an answer for this.
I don't know if its my generics that is wrong or if I'm not getting how to create this instance sintax saying .
Thank you all !
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3615
    
  14

We can't really help you, because we don't know the definition of IDao. If you post that code, we might be able to help you a little bit more.

Also, show us what you already have tried.
Vinicius Alves
Greenhorn

Joined: Dec 21, 2010
Posts: 14
Stephan van Hulst wrote:We can't really help you, because we don't know the definition of IDao. If you post that code, we might be able to help you a little bit more.

Also, show us what you already have tried.




I'm sorry, I forgot posting the IDao iterface, but here it is.
If you can help me with what I'm doing wrong it will be great !
thank you.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3615
    
  14

The type parameter of GenericSearch expects a type that extends IDao, and is further parameterized with a supertype of ModelObject.

An example would be GenericSearch<IDao<ModelObject>> gs = new GenericSearch<>();.

If you mean to make an instance that uses Fornecedor in the second inner type parameter, you can't.

Can you explain what you're trying to do? Why does GenericSearch have this signature? What is its purpose?
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
Vinicius Alves wrote:My question is , How do I create a instance of GenericSearch ?

You can do it in this way:
This will compile.
If this doesn't resolve your doubts, please explain shortly what you are trying to achieve with this code - what is your requirement ?
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3615
    
  14

Don't do it that way though, you're using unchecked conversions.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7677
    
  19

Stephan van Hulst wrote:The type parameter of GenericSearch expects a type that extends IDao, and is further parameterized with a supertype of ModelObject.
Can you explain what you're trying to do? Why does GenericSearch have this signature? What is its purpose?

Agreed, and the specific questions Vinicius has to answer are:

1. Can GenericSearch be used on ANY IDao, or only on IDaos that take some kind of ModelObject?
If the latter, then it should probably be
public class GenericSearch<T extends IDao<? extends ModelObject>> {
If not, then
public class GenericSearch<T extends IDao<?>> {

2. Can IDao be used with ANY object or only with ones that extend ModelObject?
If so, then IDao should probably be defined as
public interface IDao<T extends ModelObject> {
and GenericSearch could be defined as in the 2nd example above.

It also strikes me that IDaoSearch might be a better name for the class - in fact, if it was me, I'd be tempted to just make it a static nested class called 'Search' - but that's more a matter of taste.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Vinicius Alves
Greenhorn

Joined: Dec 21, 2010
Posts: 14
Winston Gutkowski wrote:
Stephan van Hulst wrote:The type parameter of GenericSearch expects a type that extends IDao, and is further parameterized with a supertype of ModelObject.
Can you explain what you're trying to do? Why does GenericSearch have this signature? What is its purpose?

Agreed, and the specific questions Vinicius has to answer are:


I'm creating a Create, Edit , New and Delete GUI wich will take a IDao object to perform those actions (Create, Edit , New and Delete), and the reason IDao has a ModelObject type parameter is because I neet on GenericSearch
window when I access the t.editar(ModelObject v) method as an example it will give me the type on the parameter instead of t.editar(? v);


1. Can GenericSearch be used on ANY IDao, or only on IDaos that take some kind of ModelObject?
If the latter, then it should probably be
public class GenericSearch<T extends IDao<? extends ModelObject>> {
If not, then
public class GenericSearch<T extends IDao<?>> {


Cant't be this way:


because on GenericSearch window I need

t.editar(ModelObject v); instead of t.editar(? v);


2. Can IDao be used with ANY object or only with ones that extend ModelObject?
If so, then IDao should probably be defined as
public interface IDao<T extends ModelObject> {
and GenericSearch could be defined as in the 2nd example above.


Only with the ones that extends ModelObject.


I hope I could explain well, since english is not my first language.
thank you all for helping.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3615
    
  14

I think you need to explain exactly what the responsibilities of GenericSearch are. Let's say I have an instance of GenericSearch. What does that mean?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help with Generics