Re: problem in overwrite using Generics



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
.



Relevant Pages

  • Re: How to use generics?
    ... getting rid of the ACollection and BCollection and just having Collectionwould be a good start towards using generics to the full extent. ... public void AddCollection() ... public bool ContainsKey ...
    (microsoft.public.dotnet.languages.csharp)
  • program challenge
    ... public void setName{ ... the salaried class that extends Employee ... public class Salaried extends Employee ... // Programmer.java: the programmer class that extends from Hourly ...
    (comp.lang.java.help)
  • Re: Tricky form problem
    ... > ShowDialog method) different modal child forms. ... when using ShowDialog() there's no ... > public class MainForm: Form ... > public void ShowForm() ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: InvalidOperationException was unhandled
    ... > public Vehicle MyVehicle; ... >public class Vehicle ... > public void SerializeOriginal(String filename) ...
    (microsoft.public.dotnet.vjsharp)
  • RE: Problem: C# custom event is null
    ... public event MyDelegate OnFire; ... public void B_Event_Raised ... (these are sample classes to illustrate) ...
    (microsoft.public.dotnet.languages.csharp)