The two add
methods in this class have the same erased signature:
class extend
{
Integer add (Integer a, Integer b)
{
return a + b;
}
<Type extends Integer> Type add (Type a, Type b)
{
return a + b;
}
}
This makes it impossible to have them both in the same class. The compiler reports the following error:
extend.java:8: error: name clash: add(Type,Type) and add(Integer,Integer) have the same erasure Type add (Type a, Type b) ^ where Type is a type-variable: Type extends Integer declared in method add(Type,Type)
But if they are equivalent, why is the unboxing not done in the second case. The compiler reports the following error:
extend.java:10: error: incompatible types return a + b; ^ required: Type found: int where Type is a type-variable: Type extends Integer declared in method add(Type,Type)
In the first case the compiler knows the erased type and in the second case he forgets it again? Why?