calling Overloaded method from a generic method.

Posted by asela38 on Stack Overflow See other posts from Stack Overflow or by asela38
Published on 2010-03-28T04:10:09Z Indexed on 2010/03/28 4:13 UTC
Read the original article Hit count: 408

Filed under:
|

How to create a generic method which can call overloaded methods? I tried but it gives a compilation error.

Test.java:19: incompatible types found : java.lang.Object required: T T newt = getCloneOf(t); ^

import java.util.*;

public class Test {


 private Object getCloneOf(Object s) {
  return new Object();
 }

 private String getCloneOf(String s) {
  return new String(s);
 }

 private <T> Set<T> getCloneOf(Set<T> set){
  Set<T> newSet = null;
  if( null != set) {
   newSet = new HashSet<T>();
   for (T t : set) {
    T newt = getCloneOf(t);
    newSet.add(newt);
   }
  }
 }

}    

© Stack Overflow or respective owner

Related posts about java

Related posts about generics