This, for example, is useful for allowing subclasses to use their own type.
Imagine a class like
class Node { Node next; }
if you extend this class, you are stuck in Node .
class SpecialNode extends Node { void foo() {
You also cannot just define it as
class Node<T> { T next; }
because it will allow anything for T Do you really want T to be extends Node , or you can no longer use T like Node from Node without casting. However, this will work for child classes.
Using recursive bounds such as
class Node<T extends Node<T>> { T next; }
You restrict T to yourself or subclassing yourself, which then allows you to do
class SpecialNode extends Node<SpecialNode> { void foo() { SpecialNode nextNode = this.next;
Thus, both the parent and child classes can access everything at their level of abstraction, completely typeafe.
source share