I have a regular interface (which I donβt want to make common) using the generic Get method and the generic class that implements it.
@Override does not give me a warning, and the code works as intended, but I have a warning in Foo # Get (): Type safety: The return type T for Get() from the type Test.Foo<T> needs unchecked conversion to conform to TT from the type Test.Attribute
Should I make Attribute common interface? I try to avoid manually messing with Object and casts and store all types of various attributes in a list.
(using static only to compile a test sample in one file - it does not change anything)
import java.util.ArrayList; import java.util.List; public class Test { static interface Attribute { <TT> TT Get(); } static class Foo<T> implements Attribute { T val; public Foo(T val) { this.val = val; } @Override public T Get() { System.out.println("it is me"); return val; } } public static void main(String[] args) { List<Attribute> list = new ArrayList<Attribute>(); list.add(new Foo<String>("test")); String s = list.get(0).Get(); System.out.println(s); } }
source share