views:

76

answers:

3

I saw some code on an unrelated question but it got me curious as I never saw such construct with Java Generics. What would be the use of creating a generic class that can take as type argument itself or descendants of itself. Here is example :

abstract class A<E extends A<E>> {
    abstract void foo(E x);
}

the first thing that came to mind would be a list that takes a list as parameter. Using this code feels strange, how do you declare a variable of type A ? Recursive declaration !?

Does this even work ? If so did any of you see that in code ? How was it used ?


EDIT

Indeed it turns out my question is the same as this one only phrased differently, but the answers for that question will answer mine as well.

Also thanks for the reference to the Curiously Recurring Template Pattern which gives some historical background and further explanations on the subject.

This old blog entry probably gives the best all around explanation I found for us Java guys.

Now hard to choose a right answer here for they are all helpful, so I will go for the one that ended up yielding the most reading material (referenced above)

+4  A: 

Without this, the parameter to the method foo could not be bound to type E.

If you have an implementation B of this abstract class, you can now enforce that method foo also requires its parameter to be of type B.

 class B extends A<B> {
      void foo (B x){}
 }

Without that, foo would have to take any kind of A.

I agree that the syntax for this is less than elegant.

Thilo
+1  A: 

Generics aren't just for containers like lists. This sort of "extends itself" type parameter is used to let the superclass refer to the subclass in places like method parameters and return types, even though no actual specific subclass is available when the superclass is compiled. It's analogous to the curiously recurring template pattern in C++.

A subclass of your example would be declared as

class Foo extends A<Foo>

and the inherited foo() method becomes

void foo(Foo x)

See how A defined a method that takes a Foo parameter even though it doesn't actually know about Foo?

Yes, this sort of thing is unusual, but not unheard of: the built-in Enum class uses a similar trick.

Wyzard
+1  A: 

You might want to take a gander at the answers to this question

Everyone