aspose file tools*
The moose likes Java in General and the fly likes How to overload generic method for specific type of template argument Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to overload generic method for specific type of template argument" Watch "How to overload generic method for specific type of template argument" New topic
Author

How to overload generic method for specific type of template argument

Dorian Gray
Greenhorn

Joined: May 10, 2009
Posts: 2
Hi,

I am porting C++ application to java and I have a problem with Genric method.

I have generic class which should implement functionality depending on template argument.


T should wrap plenty of different types of types and they differe in way to stream. Only method which is specific to each type is "saveToStream".
In C++ it is very easy:


As a solution I have created set of saveToStream() methods (for every type of parameter) and the same set of subclasses of generic class. It is "ugly" comparing to C++. Is there something more elegant?

BR
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19785
    
  20

Java also knows generics, but only for objects - not for the primitive types. You can use the wrapper classes however: TypeWrapper<Integer>.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Dorian Gray
Greenhorn

Joined: May 10, 2009
Posts: 2
I did it but it does not solve problem. I still need declaration of method for every argument type (primitive or wrapper around primitive)

This is copy of C++ code:


and finaly implementation for every type parameter:


In java I have to create classes for each object type I am using (Int, Boolean, String,...) and same number of methods:

Curently 8 new classes and 8 method overloads while in C++ only 8 methods are overload.

It would be good if I can create one method only where compiler will realize which concreate class to use

BR
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10535
    
    9

"Dorian Gray",
Please check your private messages for an important administrative matter.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to overload generic method for specific type of template argument