Ambiguity error in generics:

Ambiguity error occears when erasure causes two seemingly distinct generic declarations to resolve to the same erased type, causing a conflict.
Example:
class demo
{
T obj1;
V obj2;
void set(T obj1){this.obj1=obj1;}
void set(V obj2){this.obj2=obj2;}
};
Here the two overloaded methods are ambiguous.
The erasure of both the methods are same,
Void set(Object o){
So there will be compilation error like this
demo.java:5: name clash: set(T) and set(V) have the same erasure
void set(T obj1){this.obj1=obj1;}
^
demo.java:6: name clash: set(V) and set(T) have the same erasure
void set(V obj2){this.obj2=obj2;}
^
2 errors
Press any key to continue . . .

No comments:

Post a Comment