• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why use extends keyword instead of implements in Generics  RSS feed

 
Quazi Irfan
Ranch Hand
Posts: 104
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why java uses the keyword extends when setting the bound of a type parameter(Generic) to an interface. I think using the keyword implements is more intuitive.


I know if I want to set multiple bounds I will use extends keyword, and I will concatenate the bounds using & operator.

Is this a design decision to always use extends keyword to set bounds?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quazi Irfan wrote:Why java uses the keyword extends when setting the bound of a type parameter(Generic) to an interface. I think using the keyword implements is more intuitive.


So, instead of using "extends" for the upper bound (with "super" as the lower bound). You want "implements" as the upper bound and "super" as the lower bound? This doesn't seem intuitive at all (assuming that you can consider generics intuitive). Or do you want something else as the lower bound?

[EDIT] ... Sorry, you were not talking about wildcards. My bad. In that case, I don't have an opinion on how intuitive "implement" would be over "extends".

Henry
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
T is a type parameter. A type is not necessarily a class; it can also be an interface.

you should read <T extends Comparable<T>> as: a type T that extends the type Comparable<T>.

Types (in general) don't implement things, so it would not be logical to use the keyword "implements".

Classes are just one specific kind of type, the one that can implement things. Interfaces are another kind of type, and they don't implement anything.
 
Quazi Irfan
Ranch Hand
Posts: 104
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very clean explanation Jesper. Thank you very much.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!