What would be the use of accepting itself as type arguments in generics

Posted by Newtopian on Stack Overflow See other posts from Stack Overflow or by Newtopian
Published on 2010-03-30T01:27:24Z Indexed on 2010/03/30 1:33 UTC
Read the original article Hit count: 343

Filed under:
|
|

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> {
    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 ?

© Stack Overflow or respective owner

Related posts about java

Related posts about generics