Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2007

Tip: Looking for answers? Try searching our database.

problem in overwrite using Generics

Thread view: 
SantaJava@gmail.com - 30 Jan 2007 04:23 GMT
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
Lew - 30 Jan 2007 06:00 GMT
> package jp.co.nec.rfidmgr.epcis;
>
[quoted text clipped - 21 lines]
>
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.