On Thu, 14 Jul 2005 01:44:28 -0700, HK wrote:
> Consider the following interface:
>
>
> import java.util.List;
> public interface Action<CDTA> {
> void invoke(List<? extends CDTA> r);
> }
>
> Now I try to implement it like this:
>
> private static class Xaction implements Action<Number> {
> public void invoke(List<Number> l) {
> // not yet implemented
> }
> }
>
> The compiler, however, complains that Xaction does not
> implement Action. I can only guess that type-safety
> would break if the implementation was allowed. But how?
>
> Can someone give an example that goes wrong if the
> above is allowed?
Suppose someone calls your invoke method with a List<Double> Perfectly
fine with the declaration given in your interface. Now, a List<Double> is
not a List<Number>. A List<Number> can contain nothing but Integers, for
example. You can add Integers into a List<Number>, but not into a
List<Double>. Therefore, you must not "downcast" in contents types.
--
You can't run away forever,
But there's nothing wrong with getting a good head start.
--- Jim Steinman, "Rock and Roll Dreams Come Through"
|