What does <R extends TableRecord<R>> mean in Java?

穿精又带淫゛_ 提交于 2019-12-03 08:08:42

It means a class of type R, that implements the interface TableRecord<R>

TableRecord<R> means that the interface is bound to the same type R.

An example would be a class like:

public class Bla implements TableRecord<Bla>

I admit this seems a bit strange, but Java generics don't really differentiate between extends and implements, which leads to some confusion.

As to why this exact definition, I don't know enough about the context to see exactly why it makes sense, but it might be due to method signatures on the interface returning objects of type R (think Factory):

public R createTableRecord(...);
class SomeClass<R extends TableRecord<R>>

What it means that parameter type R has to be a subclass of TableRecord <R> and nothing else, i.e. you must use class

class Foo extends TableRecord <Foo>

as the parameter for defining your class SomeClass

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!