views:

63

answers:

2
interface Foo<T extends Number>{
}

class Bar<T extends Number> implements Foo<T>{

}

Why does the class have to be written that way instead of:

class Bar<T extends Number> implements Foo<T extends Number>{
}

Surely the second way is clearer.

+5  A: 

Because that's the same T, so it's redundant to say it extends Number again.

Frédéric Hamidi
+1  A: 

In the line

class Bar<T extends Number> implements Foo<T> {

T is defined at the first occurrence and used at the second. extends Number constrains the type that T can be instantiated with. You can put such constraints only at the place where T is defined.

It is similar to ordinary function parameters, where you write the type only in the declaration and not at the places where you use the parameter.

starblue