• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

compile time error - same erasure ?

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers,
I'm not able to understand the error..Please , have a look

It is giving me following error

---------- Compile ----------
TestWrapper.java:52: name clash: m1(java.util.List<java.lang.Integer>,java.util.List<java.lang.Integer> and m1(java.util.List<java.lang.Number>,java.util.List<java.lang.String> have the same erasure
public static void m1(List<Integer> l1,List<Integer> l2) {}
^
TestWrapper.java:52: name clash: m1(java.util.List<java.lang.Number>,java.util.List<java.lang.String> and m1(java.util.List<java.lang.Integer>,java.util.List<java.lang.Integer> have the same erasure
public static void m1(List<Number> l1,List<String> l2) {}
^
2 errors

I'm not getting the error...Please help..
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the point of view of the methods parameters List<Number> and List<String> are the same, because all instances of a generic class have the same run-time class (List in your case), regardless of their actual type parameters.
If you want to think of it in a veeery easy way think of that notation just like a useful casting technique that anyway does not change nothing to the signature of your methods parameters.
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i guess this is happening because the generic information is used only at compile time,due to which at runtime there will be a ambiguity in choosing which method to invoke,this is sensed up by the compiler and hence this error.

chandra am i right? and what is the consequence of this on overloading and overriding of the methods which accept such arguments,please give some light on this.
 
swapnil dangore
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all for your replies...
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


chandra am i right? and what is the consequence of this on overloading and overriding of the methods which accept such arguments,please give some light on this.



You are right Sharan. This is only because of type erasure. Parameterized types are only to restrict the use to enter the right object into the collection. All the generic information is erased by the compiler.

So after type erasure both the given method will become:


So far as Sharan's question is concerned regarding overriding see the code below to grasp the thing better.



Can you tell does EnhancedList override the LinkedList method addItem()?
Yeah you are overriding the method correctly but one gotcha is there.
The base class addItem(...) method will become :

public void addItem(Object item){...}
whereas the EnhancedList addItem will be
public void addItem(String item){...}

So it looks like it is overloading instead of overriding but it is still overriding; Compiler takes care of this and adds one bridge method to the subclass that correctly overrides the base class method and whenever call to
the subclass method is required it does so:
Let's see the bridge method added by the compiler:



That's it!

Thank you all for raising this issue.
[ May 03, 2007: Message edited by: Chandra Bhatt ]
 
I claim this furniture in the name of The Ottoman Empire! You can keep this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic