6

I'm creating an interface of JOOQ TableRecord

<R extends TableRecord<R>> 

Would anyone be able to explain the line above?

Thanks

TheKojuEffect
  • 20,103
  • 19
  • 89
  • 125
Subodh Joshi
  • 12,717
  • 29
  • 108
  • 202
  • Related: http://stackoverflow.com/q/7131714/521799, http://stackoverflow.com/q/1563335/521799, http://stackoverflow.com/q/695298/521799 – Lukas Eder Sep 19 '13 at 13:58

2 Answers2

8

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(...);
pcalcao
  • 15,789
  • 1
  • 44
  • 64
  • But in this '> ' it extends it mean 'R' is a class? – Subodh Joshi Sep 19 '13 at 10:36
  • I don't think R has to be the same type as the implementing class ([more reading)](http://stackoverflow.com/questions/18553621/how-can-i-make-an-interface-instance-method-accept-arguments-of-the-same-class-o/18554262#18554262). – jahroy Sep 19 '13 at 10:37
  • 1
    @subodh It means R is a "type", since TableRecord is an interface, R could be an interface as well. – pcalcao Sep 19 '13 at 10:42
4
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

anubhava
  • 761,203
  • 64
  • 569
  • 643