Java generics - why is "extends T" allowed but not "implements T"?

user120623 picture user120623 · Jun 10, 2009 · Viewed 175.8k times · Source

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?

Answer

Tetsujin no Oni picture Tetsujin no Oni · Jun 10, 2009

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).