4

I am relatively new to Java Programming and I came across this declaration of an interface and a class that implements it:

public interface Abcd<E extends Comparable<E>>{
......
......
......
}

public class AbcdImpl<E extends Comparable<E>> implements Abcd<E>{
......
......
......
}

Can you explain what <E extends Comparable<E>> stands for and typically what does <E> signify?

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
Jones
  • 379
  • 3
  • 9
  • 23

2 Answers2

6

E stand for a type, the letter doesn't need to be E it can be anything, but usually people will use letters like T,K,E,V. It makes sense to use a letter that somehow signifies what the type is used for. For instance in the Map interface Map<K,V>, K is for key and V is for value.

As for <E extends Comparable<E>> it means that the type E that is the generic type of AbcdImpl needs to itself implement the Comparable interface (for itself). So any class E has to have a method

compareTo(E obj)

for comparing one instance of E to another instance of E.

ilyas patanam
  • 5,116
  • 2
  • 29
  • 33
Matti Lyra
  • 12,828
  • 8
  • 49
  • 67
2

What you see is a generic type declaration.

See http://en.wikipedia.org/wiki/Generics_in_Java

Abcd<E> is normally read as "Abcd of E".

For example, List is a list of items, but it's not specified what kind of items. List is a list of strings, and is a parametrised version of the generic type List.

Joeri Hendrickx
  • 16,947
  • 4
  • 41
  • 53