Re: problem in overwrite using Generics
- From: Lew <lew@xxxxxxxxxxx>
- Date: Tue, 30 Jan 2007 01:00:49 -0500
SantaJava@xxxxxxxxx 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
.
- References:
- problem in overwrite using Generics
- From: SantaJava@xxxxxxxxx
- problem in overwrite using Generics
- Prev by Date: Re: problem in overwrite using Generics
- Next by Date: Re: JVM/Java memory footprint
- Previous by thread: problem in overwrite using Generics
- Next by thread: problem in overwrite using Generics
- Index(es):
Relevant Pages
|