1

I understand there is some backward compatibility issue, and it is not allowed by compiler.

Method has the same erasure as another method in type

how do others achieve this? Is there any other solution than to define them with different function names?

Community
  • 1
  • 1
Chakradar Raju
  • 2,691
  • 2
  • 26
  • 42
  • 4
    Please describe what your use case is. Using separate method names might be the best way, but, for example, you might also be able to use `List`. – chrylis -cautiouslyoptimistic- Aug 23 '13 at 12:39
  • i'm getting x and y coordinates as list of numbers, if it is double, i'll scale it to width or height, based on x or y. – Chakradar Raju Aug 23 '13 at 12:45
  • 3
    @ChakradarRaju That's a horrible design. Such a subtle difference in behavior shouldn't depend on the overloading difference between integers and floats. Just give the two methods different names. – Sebastian Redl Aug 23 '13 at 13:23

4 Answers4

0

this is because at bytecodelevel, the generic is removed and you have basically a list of objects.. resulting in two identical method-signatures.

but you could use Integer[] and Double[] instead if it fits your requirements because these ARE different types.

Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56
wrm
  • 1,898
  • 13
  • 24
0

you can make them generic like List and at the time of instantiation just tell what you want to do. Either declare as List or as List.

0

make a generic list like:-

List<> list= new ArrayList<>();

you can write any parameter like T,V,B or any other in the brackets and then later on on in the main you define the generic parameter either as Integer or as Double. Generally,T is used.

Read generics for better understanding.

RAS
  • 8,100
  • 16
  • 64
  • 86
-3

Probably you can take the parameter as List of Object. In the implemention, check the class type of the object passed and have a switch to process different for integer and double

Please accept/vote up, if this resolves your query

MansoorShaikh
  • 913
  • 1
  • 6
  • 19
  • Try your own suggestion, and you'll see that it doesn't fit. Hint: a List is not a List. Second hint: the class of the argument will always be List.class. – JB Nizet Aug 23 '13 at 12:39
  • Completely avoiding the generics system isn't the best way to resolve the issue, especially when the types involve share a close superclass (`Number`). – chrylis -cautiouslyoptimistic- Aug 23 '13 at 12:39
  • Sorry. Yes, I thought about this just after submitting the answer. Could not find a way to delete an answer after it gets posted. – MansoorShaikh Aug 23 '13 at 12:41