• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Name clash: Two methods have the same erasure

 
Rancher
Posts: 59
7
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have two methods that I want to overload:





The compiler error I encounter:



If I rename the one method, the code compiles, but I want to know why this is considered a name clash.

Why is the code above not an example of method overloading where the data type of the parameters is different?

How do I fix this, so that I can overload these methods?
 
Marshal
Posts: 75715
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That isn't overloading. They both have the signature saveList(List). Remember that generic information is removed at runtime, so the compiler cannot distinguish those two methods.
I am not sure what the correct answer is because you haven't provided enough information about the two methods. I am a bit suspicious, however, that you are acting outwith the List's original owning object, which means you are breaching the single responsibility principle. I have found a few places where you can find out about that: 1 2 and enclosed links. If there is an object with those Lists in, maybe that object should be responsible for maintaining those Lists. Unless you have a generic list saving utility, consider moving those methods elsewhere.
 
Sheriff
Posts: 16961
286
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's because of type erasure: https://docs.oracle.com/javase/tutorial/java/generics/erasure.html

And I think you might have gotten it backwards there, Campbell. Generic types are used by the compiler to provide tighter type checking. Type erasure will replace the generic types with ordinary types (classes, interfaces) and methods, typecasts to preserve type safety, or bridge methods to preserve polymorphism. It's the runtime that has no idea of generics, so the error is the compiler basically saying: "Oops, when I apply type erasure these two methods turn out to have the same exact signature. That's not going to work for the JVM."
 
Jay Rex
Rancher
Posts: 59
7
Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Campbell and Junilu.

Note to self: Use the search function before posting, as this has been covered already
 
Campbell Ritchie
Marshal
Posts: 75715
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jay Rex wrote:Thanks Campbell and Junilu. . . .

That's a pleasure And sorry that I worded my reply so badly.
 
I am going to test your electrical conductivity with this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic