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


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Generic question" Watch "Generic question" New topic
Author

Generic question

Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580


I want to do the following but I can't. I can only declare myGet() to return Object.

I can do this :



but it seems inelegant, as I have to repeat my declaration of the T parameter.
Is there a more elegant way?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18166
    
    8

What about this?
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
"cannot resolve symbol E".
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18166
    
    8

(paying closer attention) Then what about this?If I understand your design, a TableRow<E> is a row that is encapsulated by an E object, not a row that contains E objects? In that case a SimpleTableModel<E> is going to encapsulate a list of TableRow<E> elements.
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
unfortunately I need to access the class of TableRow too as I have a method :



Jean-Francois Briere
Ranch Hand

Joined: Mar 03, 2004
Posts: 101

I want to do the following but I can't. I can only declare myGet() to return Object.

Why are you saying that?
I don't understand as it compiles perfectly "as is".
Also did a simple test with TableRow<Integer> and works fine.
Here is the code:

Regards
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580
Essentially I want to do this :

[/qb]<hr></blockquote>

but I cannot, I have to do this :



Which is repetition and not elegant, but it seems like it is the only way.

D.
[ May 16, 2006: Message edited by: Don Kiddick ]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Do you really need to care about which kind of TableRow the TableModel is using?

If you just want an add method,



should work just fine, shouldn't it?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18166
    
    8

I still don't understand why you need <E extends TableRow> anywhere. Are you going to be working with subclasses of TableRow -- or TableRow<X> -- in this class?
Don Kiddick
Ranch Hand

Joined: Dec 12, 2002
Posts: 580

Do you really need to care about which kind of TableRow the TableModel is using?


The only reason I do is that I have a subclass of SimpleTableModel, called EditableTableModel which parameterizes TableRow with EditableTableRow.


I still don't understand why you need <E extends TableRow> anywhere. Are you going to be working with subclasses of TableRow -- or TableRow<X> -- in this class?

Both.

Here's the whole code :

TableRow :


EditableTableRow :


SimpleTableModel :


SimpleEditableTableModel:



thanks for all your input,
D.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
In that case, I don't think there is a way around

class SimpleTableModel<T, E extends TableRow<T>>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generic question
 
Similar Threads
Generics
Generics question
question about the generic methods : ExamLab final test
Doubt in generic method
Button Sizes