I wonder if there is a special reason in Java for using always "extends
" rather than "implements
" for defining bounds of typeparameters.
Example:
public interface C {}
public class A<B implements C>{}
is prohibited but
public class A<B extends C>{}
is correct. What is the reason for that?
There is no semantic difference in the generic constraint language between whether a class 'implements' or 'extends'. The constraint possibilities are 'extends' and 'super' - that is, is this class to operate with assignable to that other one (extends), or is this class assignable from that one (super).
The answer is in here :
So there you have it, it's a bit confusing, and Oracle knows it.
It's sort of arbitrary which of the terms to use. It could have been either way. Perhaps the language designers thought of "extends" as the most fundamental term, and "implements" as the special case for interfaces.
But I think
implements
would make slightly more sense. I think that communicates more that the parameter types don't have to be in an inheritance relationship, they can be in any kind of subtype relationship.The Java Glossary expresses a similar view.
Because interfaces are just classes, except they don't have attributes or implementations. The only use of the keyword "implements" is to allow a class to inherit multiple interfaces, but not multiple classes, and we can see it in the code. I don't know if they will specify this in the future, but this is not a must.
The "extends" in this context, means just like implements in interfaces and extends in classes. Here is more explanation
It may be that the base type is a generic parameter, so the actual type may be an interface of a class. Consider:
Also from client code perspective interfaces are almost indistinguishable from classes, whereas for subtype it is important.