I'm trying to grasp a better context of using generics in Java. I've got two methods, which, eventually, hit the database (serviceCall.findAll method) and retrieve a set of either DBClass1 (from one database table) or DBClass2 (from another table).
As you can see, both methods are, pretty much exactly the same, except for the return type - a set of different classes, which represent the database record (the classes have the same properties, they just, as I've already mentioned, reprensent results
from different tables). Is there a way, perhaps using generics, to use one method instead, where I can pass some kind of a flag that will tell it which set to return? Thanks in advance.
Darryl Burke wrote:Do DBClass1 and DBClass2 extend a common supertype?
What does serverCall.findAll(ids) return?
To answer your first question - no. It would be simpler if they would, of course.
Now, your second question: findAll methods return the list of DBClass1 and the list of DBClass2, respectively. I should've been clearer, I do apologize. The instances "serviceCall" are different service calls and should be named differently. So, bottom line: one returns the list of DBClass1 objects, another one - list of DBClass2 objects.
Darryl Burke wrote:Given that scenario, I can't see a way to create a single typesafe method. In the absence of a common superclass (other than Object) Generics becomes irrelevant.
Well, this seemed to work. Seems rather dirty (like explicit casting to T), but that's all I could think of. If anyone knows a better way, it'll be great. ;)
BTW There is no advantage to using generics in this case,