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?
The extends always precedes the implements keyword in any Java class declaration. When the Java compiler compiles a class into bytecode, it must first look to a parent class because the underlying implementation of classes is to point to the bytecode of the parent class - which holds the relevant methods and fields.
Java Generic Classes and SubtypingWe can subtype a generic class or interface by extending or implementing it. The relationship between the type parameters of one class or interface and the type parameters of another are determined by the extends and implements clauses.
Yes, you can. But you need to declare extends before implements : public class DetailActivity extends AppCompatActivity implements Interface1, Interface2 { // ... }
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 :
To declare a bounded type parameter, list the type parameter's name, followed by the
extends
keyword, followed by its upper bound […]. Note that, in this context, extends is used in a general sense to mean eitherextends
(as in classes) orimplements
(as in interfaces).
So there you have it, it's a bit confusing, and Oracle knows it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With