3

I have a method. It accepts a param, it can either be an object of the Provider or String class. Now if I tell that method to accept Object then I can pass it Provider or String object, but I want to make it type safe using generics and pass only either Provider or String. How do I do that, is it possible?
I want to achieve something like this:

public <T can be String or Provider> void myMethod(T value)

I had a look at this question, but it would not work in my case because none of Provider or String is an interface.

Community
  • 1
  • 1

3 Answers3

7

You should not (and can not) use generics for this. Instead simply provide two overloaded method:

public void myMethod(String value);
// and
public void myMethod(Provider value);

Since both need to have some different handling anyway it's actually simpler this way.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • I had thought about the overloading option, but because both would have the same handling in my method, so I was wondering if this can be done via generics. If it cannot be done then I will use overloading now. Thank you for the answer. – Harshad Vyawahare Oct 12 '11 at 11:14
  • 1
    @HarshadVyawahare: *how* can you handle `String` and `Provider` in the exact same way? They (probably) don't share any interesting methods. What do you actually *do* with those objects? Can you give an example where you'd handle them the same way? – Joachim Sauer Oct 12 '11 at 11:15
  • from myMethod I am calling another method, which is overloaded to accept either 'Sting' or 'Provider'. That is the only use I make of those 2 objects. – Harshad Vyawahare Oct 12 '11 at 11:20
  • 2
    So you *do* call two different methods (yes, overloaded methods are still distinct methods). You couldn't do that with a single method anyway (or you'd need an ugly `if (value instanceof String)` check). Instead move the common code of the two methods in another (possibly private/protected) method and leave just the call to the other overloaded method in the public method together with the call to the "real code" method. – Joachim Sauer Oct 12 '11 at 11:23
  • I thought if I pass a `String` or a `Provider` to this method then java will decide at runtime which overloaded method to call, and then I do not have to use `instanceof`. Both have roughly the same implementation. I will use overloading as you mentioned. – Harshad Vyawahare Oct 12 '11 at 11:34
  • 4
    @HarshadVyawahare: Java *doesn't* decide this at runtime: the choice between overloaded methods is *always* decided at compile-time. Only *overriden* methods can lead to different code being execute at runtime. – Joachim Sauer Oct 12 '11 at 11:42
1

Not, that's not possible. How about simply having two overloaded methods instead?

public void myMethod(String value)
public void myMethod(Provider value)
Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
0

Why exactly "Provider" or "String"? What do they have in common that makes your method work for both of them? For instance, are both Comparable? Then you should use Comparable in there. If they do not share something in common, maybe there is a reason that you cannot do what you are trying to do. In that case, just use overloading, as others suggested.

Janick Bernet
  • 20,544
  • 2
  • 29
  • 55