Java why is class generic
However, the ability to use generic typed methods is the correct answer; it's just not clear from the given example here. Chris Arguin Chris Arguin Bill the Lizard Bill the Lizard k gold badges silver badges bronze badges.
Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Does ES6 make JavaScript frameworks obsolete? Podcast Do polyglots have an edge when it comes to mastering programming Featured on Meta. Now live: A fully responsive profile. Related Hot Network Questions. You can write a single generic method declaration that can be called with arguments of different types.
Based on the types of the arguments passed to the generic method, the compiler handles each method call appropriately. Each type parameter section contains one or more type parameters separated by commas. A type parameter, also known as a type variable, is an identifier that specifies a generic type name.
The type parameters can be used to declare the return type and act as placeholders for the types of the arguments passed to the generic method, which are known as actual type arguments. A generic method's body is declared like that of any other method. Note that type parameters can represent only reference types, not primitive types like int, double and char. There may be times when you'll want to restrict the kinds of types that are allowed to be passed to a type parameter.
Kotlin doesn't have these. Instead, Kotlin has declaration-site variance and type projections. Let's think about why Java needs these mysterious wildcards. If List were not invariant , it would have been no better than Java's arrays, as the following code would have compiled but caused an exception at runtime:. Java prohibits such things in order to guarantee run-time safety.
But this has implications. For example, consider the addAll method from the Collection interface. What's the signature of this method? Intuitively, you'd write it this way:. That's why the actual signature of addAll is the following:. The wildcard type argument?
This means that you can safely read E 's from items elements of this collection are instances of a subclass of E , but cannot write to it as you don't know what objects comply with that unknown subtype of E. In other words, the wildcard with an extends -bound upper bound makes the type covariant.
The key to understanding why this works is rather simple: if you can only take items from a collection, then using a collection of String s and reading Object s from it is fine. Joshua Bloch gives the name Producers to objects you only read from and Consumers to those you only write to. He recommends:. But Java does not know this, and still prohibits it:.
Doing so is meaningless, because you can call all the same methods on such a variable as before, so there's no value added by the more complex type. But the compiler does not know that. In Kotlin, there is a way to explain this sort of thing to the compiler. To do this, use the out modifier:.
In other words, you can say that the class C is covariant in the parameter T , or that T is a covariant type parameter.
The out modifier is called a variance annotation , and since it is provided at the type parameter declaration site, it provides declaration-site variance.
0コメント