title: Java: One function with many return types... Is it possible with generics?
body: <p>I have a few procedures that, for simplicity's sake, look like the following:</p>
  
  <pre><code>public String fetchValueAsString(String key);
  public DateTime fetchValueAsDateTime(String key);
  </code></pre>
  
  <p>I want something like</p>
  
  <pre><code>public &lt;X is a String or a DateTime&gt; X fetchValue(String key);  // pseudo-code
  </code></pre>
  
  <p>that I could call like this (without casting; the type is implied by the passed parameters):</p>
  
  <pre><code>String str = fetchValue("subject");
  DateTime dt = fetchValue("startDate");
  </code></pre>
  
  <p>I know I could just have one method that returns the Object type and just do a casting conversion, but I'm wondering if there is a way I can have just one method called, and somehow use generics to determine the return value. So, is it possible in Java (<a href="https://stackoverflow.com/questions/392931/one-function-with-varying-return-types-possible-with-generics">it is in C#</a>)?</p>