Hi Lennie
You can't get the type inside the buildSheet method because buildSheet
itself declares the generic.
I think what you want to do is declare the generic in your abstract class
instead of in the buildSheet method, then specify the type when you extend
the class. Then you would be able to work with the specified type in the
sub-class's buildSheet method.
It would look something like this: (coding off the top of my head so the
syntax may be a bit off)
public abstract class X<T>
{
protected abstract ReportSheet buildSheet(SheetParameter<T>
sheetParameter) throws Exception;
}
public class Y extends X<String>
{
protected ReportSheet buildSheet(SheetParameter<String> sheetParameter)
throws Exception
{
String s = sheetParameter.get();
}
}
Hope this helps
Cheers
Gary
On Mon, Sep 8, 2008 at 2:05 PM, Lennie De Villiers <[EMAIL PROTECTED]>wrote:
>
> Hi,
>
> I've created a generic:
>
> public class SheetParameter<T> {
> private T value;
>
> public SheetParameter(T value)
> {
> this.value = value;
> }
>
> public void set(T value)
> {
> this.value = value;
> }
>
> public T get()
> {
> return this.value;
> }
> }
>
> here I want T to be off any type to be determined when I create it, I
> then create it:
>
> sheets.add(buildSheet(new SheetParameter<String>("lalalala")));
>
> so then T is of type String. I then need to pass that type to the
> buildSheet method that I declared as:
>
> protected <T> ReportSheet buildSheet(SheetParameter<T> sheetParameter)
> throws Exception
>
> but when i call get() method its of type T and is unknown, I must then
> cast it to String. Am I doing something wrong? How can I change the
> buildSheet method above where it must take the type of the
> SheetParameter that I created (in this case the type is String). The
> idea is that the method above is in an abstract class so can be
> overridden and then you can pass any SheetParameter type to it.
>
> Thanks
>
> Lennie De Villiers
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"CTJUG Tech" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.co.za/group/CTJUG-Tech?hl=en
-~----------~----~----~----~------~----~------~--~---