Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why JAVA can not compile when using generic type in interface

Tags:

java

generics

Hello I have a problem using generic types in java. The actual problem is that when I am using generic type on a method declaration is working fine. Then if I a add a generic type to the interface declaration the code does not compile. Following is the working code:

public interface IRuntimeConvert {
    public <T> T convertInstanceOfObject(String o, Class<T> clazz);
}

public class RuntimeConvertImpl implements IRuntimeConvert {

    private final Map<String, Object> hashMap;

    public RuntimeConvertImpl(Map<String, Object> hashMap) {
        this.hashMap = hashMap;
    }

    @Override
    public <T> T convertInstanceOfObject(String o, Class<T> clazz) {
        try {
            return clazz.cast(hashMap.get(o));
        } catch (ClassCastException e) {
            return null;
        }
    }
}

public static void main(String[] args) {

    Map<String, Object> hashMap = new HashMap<>();
    hashMap.put("s", "string");
    hashMap.put("i", 0);
    hashMap.put("l", 0L);

    IRuntimeConvert rtConvert = new RuntimeConvertImpl(hashMap);

    String s = rtConvert.convertInstanceOfObject("s", String.class);
    System.out.println(s);
    Integer i = rtConvert.convertInstanceOfObject("i", Integer.class);
    System.out.println(i);
    Long l = rtConvert.convertInstanceOfObject("l", Long.class);
    System.out.println(l);
}

The above code is compiling. When I made the following changes the code is not compilable.

public interface IRuntimeConvert<S> {

    public <T> T convertInstanceOfObject(String o, Class<T> clazz);

    public S getSomething(S s);
}

public class RuntimeConvertImpl implements IRuntimeConvert<Object> {

    private final Map<String, Object> hashMap;

    public RuntimeConvertImpl(Map<String, Object> hashMap) {
        this.hashMap = hashMap;
    }

    @Override
    public <T> T convertInstanceOfObject(String o, Class<T> clazz) {
        try {
            return clazz.cast(hashMap.get(o));
        } catch (ClassCastException e) {
            return null;
        }
    }

    @Override
    public Object getSomething(Object s) {
        throw new UnsupportedOperationException("Not supported yet."); 
    }

}

After the above changes in the main class I encouted a type cast error for each invocation of the method rtConvert.convertInstanceOfObject("s", String.class) with error message :

Incompatible types: Object cannot be converted to String.

If I try to run the main I get: Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - incompatible types: java.lang.Object cannot be converted to java.lang.String

like image 653
Chris Avatar asked Feb 12 '26 11:02

Chris


1 Answers

This happens because you have specified the generic type in your implementation to be Object with the following line:

public class RuntimeConvertImpl implements IRuntimeConvert<Object> {

As such, a call to getSomething will always have to return an Object. However, since String is a subclass of Object, this can not be automatically cast to a String, you'd have to do something like:

String s = (String) rtConvert.getSomething();

Note that this does not guarantee type safety and may lead to ClassCastExceptions.

like image 66
TR4Android Avatar answered Feb 16 '26 20:02

TR4Android



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!