• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

reference to reset is ambiguous

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


when i try this code i gt following refernce to rest is ambiguous
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't get that. I get that the reference to "reset" is ambiguous...and a whole lot more.

It is INCREDIBLY helpful if you post the full, complete error message. This tells me an awful lot about what's wrong, as opposed to your incorrect paraphrase:

 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As the error message Fred posted says, the WordBox class has two reset methods. They are:

- public void reset(String) inherited from Box<String>
- public void reset(S) defined in WordBox<S>

When you create a WordBox<String> that means that you've effectively got two reset(String) methods. The real methods, because of type-erasure, are reset(Object) and reset(CharSequence), which is why this can actually happen. But the method matching rules will consider them both a reset(String), and so it can't make a decision.

Actually quite an interesting example, because the "ambiguous" error usually needs at least two arguments without this sort of trick.
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it would make a lot more sense to either have

or

But the current code mixing the WordBox<S extends CharSequence> with the Box<String> seems... inconsistent. And confusing. And is probably responsible for the ambiguity.
 
Kasun Wixkramanayake
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thans mathews for your answer.can you explain The real methods, because of type-erasure, are reset(Object) and reset(CharSequence), little bit more
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you heard of type erasure?

When you have a generic class like List<String>, at runtime the generic type isn't used. The JVM treats it as a List. The generic type information is used by the compiler to make sure all calls are safe, but it doesn't exist in the byte code*. This was done so that generic classes were backwards compatible with pre-generic classes - it's a bit of a pain at times, but that was the decision.

So when you write a class:

At runtime T is just treated as Object and it's more like:


In the WordBox class, the difference is that S is declared to extend CharSequence. The erasure of S (meaning the class S is considered to be at runtime) can therefore be more specific than Object, and is CharSequence. So the class gets translated as:



You can see the difference this makes if you delete the extends CharSequence part. Then I get the following error (may depend on your compiler):

name clash: reset(S) in test.WordBox and reset(T) in test.Box have the same erasure, yet neither overrides the other


"Have the same erasure" is what I was talking about. The compiler knows they are different methods, so it doesn't consider this to be an override, but they'll both actually compile down to the same signature, which isn't allowed.


(* Technically I believe it does, but not in a way that makes a difference here)
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:But the current code mixing the WordBox<S extends CharSequence> with the Box<String> seems... inconsistent. And confusing.



I suspect this is an artificial example created precisely to show off this ambiguity.
 
Kasun Wixkramanayake
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you mathew for your great explanation.
 
reply
    Bookmark Topic Watch Topic
  • New Topic