What does this Java generics paradigm do and what is it called?

Posted by Tom on Stack Overflow See other posts from Stack Overflow or by Tom
Published on 2010-03-04T21:40:49Z Indexed on 2010/03/30 18:23 UTC
Read the original article Hit count: 93

Filed under:
|
|

I'm looking at some Java classes that have the following form:


public 
abstract
class A <E extends A<E>> implements Comparable <E> {

   public final int compareTo( E other ) {
      //  etc
   }   
}

public 
class B extends A <B> {
   // etc
}

public 
class C extends A <C> {
   // etc
}


My usage of "Comparable" here is just to illustrate a possible use of the generic parameter "E". Does this usage of generics/inheritance have a name? What is it used for?

My impression is that this allows the abstract class to provide a common implementation of a method (such as compareTo) without having to provide it in the subclasses. However, in this example, unlike an inherited method it would restrict subclasses to invoking compareTo on other instances of the same subclass, rather than any "A" subclass. Does this sound right?

Anyway, just curious if any gurus out there have seen this before and know what it does.

Thanks!

© Stack Overflow or respective owner

Related posts about java

Related posts about generics