Pergunta

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?

Foi útil?

Solução

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.

Outras dicas

In java, a generic class/interface has a fixed number of generic parameters. It's just the way the language is defined.

The closest thing to what you are talking about might be:

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

an instance of J is still assignable to a variable of type I.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top