public Integer add(Integer i, Integer j){return i+j;}
public String add(String i, String j){return i+j;}
public <T> T add(T i, T j){return i+j;} //this gives me error.
Although the methods look nearly identical, in this case, generics cannot help. Consider this:
public <T> T add(T i, T j){return i+j;}
...
add(new Object(), new Object());
What does it mean to add to just regular Objects together? No, + is not supported for Object and is not supported for many other types that could be substituted for T.
In creating a generic method <T> T add(T i, T j), the generic method is restricted to methods and operations that are allowed on the generic type parameter T, which effectively means only things that you can do with Object. You are trying to use a method/operation +, which is not something you can do with Object.
We could try to solve this problem by finding a common base class of String and Integer that supports all the methods and operations we have to use in our generic method body. If there were such a common base class XXXXX that supported the + operator, we could do this:
public <T extends XXXXX> T add(T i, T j) { return i+j; }
However, there is no common base class XXXXX that supports everything we want to do in the generic method (+), so generics can't be used to solve this problem.