java - Generics implementation using interface with generics -
i have interface value , class record
public interface value<t> { t getrawvalue(); } public class record<value<t>> { private value<t> value; t getrecordrawvalue() { return value.getrawvalue(); } }
java not compile , complains generics declaration > . enlighten me why invalid syntax?
you need bound generic type indicate requirement of being value<t>
, , need preserve type value value<t>
providing, therefore record
class requires 2 generic types: t: type of values returned , u: type represents value<t>
public class record<t, u extends value<t>>
here have full example of this:
public interface value<t> { t getrawvalue(); } public class record<t, u extends value<t>> { public record(u value) { this.value = value; } private u value; t getrecordrawvalue() { return value.getrawvalue(); } } public class stringvalue implements value<string> { @override public string getrawvalue() { return "raw"; } } public class strongvalue implements value<string> { @override public string getrawvalue() { return "rrrrraaawww"; } } public class stringrecord extends record<string, stringvalue> { public stringrecord(stringvalue valueprovider) { super(valueprovider); } public string report() { return super.getrecordrawvalue(); } }