Home >Java >javaTutorial >Why is \'extends T\' allowed for type parameter bounds in Java, but not \'implements T\'?
Question:
In Java, why is "extends T" permitted for defining type parameter bounds, but "implements T" is not allowed?
For instance, the following code is prohibited:
<code class="java">public interface C {} public class A<B implements C> {}</code>
While this code is valid:
<code class="java">public class A<B extends C> {}</code>
Answer:
Semantically, there is no distinction between "extends" and "implements" within the generic constraint language. The constraint possibilities are limited to "extends" and "super," reflecting the direction of inheritance or assignment compatibility.
In the case of the invalid code example, it attempts to use "implements" to define a constraint on the type parameter B. However, "implements" is not a valid constraint type and is therefore not permitted.
The above is the detailed content of Why is \'extends T\' allowed for type parameter bounds in Java, but not \'implements T\'?. For more information, please follow other related articles on the PHP Chinese website!