Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't Java allow overloads based on type parameters?

We can get away with this in .NET:

interface I<A> {}
interface I<A, B> {}

... but in Java, the same code will result in a compilation error.

That's interesting, given that even if the type information is gone at runtime, one would expect the information about the number of type parameter to still be there.

If this limitation is related to type erasure, can someone explain why?

like image 440
J Smith Avatar asked Jul 11 '13 01:07

J Smith


1 Answers

It's not related to type erasure so much as to the ambiguity that would arise from using the raw type:

I eye = null;  // which 'I' is it?

Raw types are allowed in order to accommodate code written before generics were introduced in JDK 5.0.

like image 98
arshajii Avatar answered Sep 22 '22 16:09

arshajii