Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help with Generics

 
Vinicius Alves
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 5574
53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 5574
53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 5574
53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't do it that way though, you're using unchecked conversions.
 
Winston Gutkowski
Bartender
Pie
Posts: 10236
58
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Vinicius Alves
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 5574
53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic