Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

jooq

I'm creating an interface of JOOQ TableRecord

<R extends TableRecord<R>> 

Would anyone be able to explain the line above?

Thanks

like image 419
Subodh Joshi Avatar asked Sep 19 '13 10:09

Subodh Joshi


2 Answers

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(...);
like image 64
pcalcao Avatar answered Sep 22 '22 21:09

pcalcao


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

like image 33
anubhava Avatar answered Sep 23 '22 21:09

anubhava