Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > problem in overwrite using Generics

Reply
Thread Tools

problem in overwrite using Generics

 
 
SantaJava@gmail.com
Guest
Posts: n/a
 
      01-30-2007
package jp.co.nec.rfidmgr.epcis;

import java.util.List;
import java.util.Map;

public class T {
public void temp(List<String> str) {

}

public void temp(List<Integer> str) {

}

public void temp(List<Map<Integer,String>> str) {

}
}

error:
Duplicate method temp(List<String>) in type T T.java
Duplicate method temp(List<Integer>) in type T T.java
Duplicate method temp(List<Map<Integer,String>>) in type T T.java

how can i overwrite a method using generics

 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      01-30-2007
wrote:
> package jp.co.nec.rfidmgr.epcis;
>
> import java.util.List;
> import java.util.Map;
>
> public class T {
> public void temp(List<String> str) {
>
> }
>
> public void temp(List<Integer> str) {
>
> }
>
> public void temp(List<Map<Integer,String>> str) {
>
> }
> }
>
> error:
> Duplicate method temp(List<String>) in type T T.java
> Duplicate method temp(List<Integer>) in type T T.java
> Duplicate method temp(List<Map<Integer,String>>) in type T T.java
>
> how can i overwrite a method using generics


Do you mean "override" or "overload"? It is not clear from the context which
standard term you intend.

The three seemingly overloaded methods you show cannot be overloads because of
"type erasure". That means that two methods whose signatures differ only in
the "generic part" really have the same signature. Two methods with the same
signature cause a "Duplicate method" compilation error.

Does each method perform a different algorithm depending on the List's base
type? If so, your answer might be inheritance and overriding the method:

public interface Tempus<T>
{
public void temp( List<T> x );
}

public class StringTempus implements Tempus<String>
{
public void temp( List<String> x )
{
...
}
}

public class IntegerTempus implements Tempus<Integer>
{
public void temp( List<Integer> x )
{
...
}
}

Or, it may be that you are performing essentially the same list operation
independent of type, in which case you just declare a generic type:

public class TempusImpl<T> implements Tempus<T>
{
public void temp( List<T> x )
{
...
}
}

Note that both approaches can coexist.

- Lew
 
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
USB key overwrite - using DD ? BertieBigBollox@gmail.com Computer Support 3 09-14-2009 09:02 PM
generics depending on generics Soul VHDL 0 02-02-2009 09:14 AM
problem in overwrite using Generics SantaJava@gmail.com Java 1 01-30-2007 05:42 AM
problem in overwrite using Generics SantaJava@gmail.com Java 0 01-30-2007 04:23 AM
Can't convert a generics list of objects into a generics list ofinterfaces Juergen Berchtel Java 1 05-20-2005 02:07 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57