Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > wildcards in generic method of an interface

Reply
Thread Tools

wildcards in generic method of an interface

 
 
HK
Guest
Posts: n/a
 
      07-14-2005
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?

Harald.

 
Reply With Quote
 
 
 
 
Stefan Schulz
Guest
Posts: n/a
 
      07-15-2005
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"


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
generic interfaces with generic methods Murat Tasan Java 1 02-03-2009 12:17 PM
Interface Method with Interface Parameter Edward.Shtern@gmail.com Java 4 11-02-2007 05:36 PM
ObjectDataSource & FormView adding two extra paramaters to Update method giving error non-generic method ... Fred Dag ASP .Net 0 09-18-2006 10:36 PM
Generic class in a non generic class nramnath@gmail.com Java 2 07-04-2006 07:24 AM
Java 5, Generic, Map and wildcards Benjamin Lerman Java 1 03-09-2006 01:03 PM



Advertisments