java - How to implement a generic recursive interface? -
another case of java generics confusion...
i defining hierarchy of interfaces must include method return list of interface type. standard use case recursive generic pattern (or whatever name is):
interface superinter<t extends superinter<t>> { list<t> geteffects(); }
now when extends interface:
interface subinter extends superinter<subinter> {}
i can implement sub-interface , have correct method implement:
class subimpl implements subinter { @override public list<subinter> geteffects() { return null; } }
and other interface uses generic type have implementing class contain method returns list of interface.
however, can't implement super interface type correctly:
class superimpl implements superinter<superinter> { @override public list<superinter> geteffects() { return null; } }
besides raw types warning, get:
bound mismatch: type superinter not valid substitute bounded parameter <t extends superinter<t>> of type superinter<t>
i guess because class not extends itself. how can achieve this?
you can declare follows:
class superimpl implements superinter<superimpl> { @override public list<superimpl> geteffects() { return null; } }
Comments
Post a Comment