Alex Rivera | Logout

Why is "extends T" allowed but not "implements T"?

Asked 2009-06-10T15:42:33.507
382

Is there a special reason in Java for using always "extends" rather than "implements" for defining bounds of type parameters?

For 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?

Edit
Report

2 Answers

350

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

answered 2009-06-10T15:45:51.503
8

Here is a more involved example of where extends is allowed and possibly what you want:

public class A<T1 extends Comparable<T1>>

answered 2012-07-31T13:48:57.567

Your Answer