You can do this by checking the class of the object to see if they are the same
public void method(Object foo, Object bar) {
if(!foo.getClass().equals(bar.getClass())) {
throw new IllegalArgumentException("Exception");
}
}
, java Object .
:
public <T> void method(T foo, T bar, Class<T> clazz) {
}
:
method("string 1", "string 2", String.class);